How to avoid the use of session variables in this script

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


I have the following code snippet:

public   function register($name, $rule, $dat_file, $server =
        if(in_array($name, $this->m_names))
            $this->m_rules[$name][] = $rule;
            return $_SESSION[$name];

            // $cmd = "$$name = new GTCM_GNP($dat_file, $server, \
        //    eval($cmd);

        $$name = new GTCM_GNP($dat_file, $server, $port);
          return FALSE;
        $this->m_names[] = $name;
        $this->m_rules[$name] = array($rule);
        return $$name;

 What exactly does "return $_SESSION[$name]" do? it looks like it just
returns the $name argument in the function list. Or does it save it as
a session variable also? Why not just do:  "return $name;" ?? Is there
a better way to do this without using session variables or super
globals? $_SESSION was originally $GLOBALS changing it to $_SESSION
did not break it and it seemed like a better alternative but probably
not the best way. By the way this code was originally written for php
4.0.6 if that makes any difference.



Re: How to avoid the use of session variables in this script


Quoted text here. Click to load it

The _SESSION super global refers to a standardized PHP cookie.  
Somewhere in the init code there should be a call to session_start().
What is going on 'under the hood' is that a cookie named PHPSESSID (or
something like that) is created and initialized.  Setting
$_SESSION[<mumble>] to some value (where <mumble> is some arbitary key)
is a way of saving values across pages.  The contents of the _SESSION
array is used to initialize the cookie when the headers are sent.  When
some future page is accessed by the web browser that received this
cookie, it passes the cookie in the headers and session_start()
retrieves the cookie and re-initializes the _SESSION array from the
contents of the cookie.

The book "Learning PHP, MySQL & JavaScript", by Robin Nixon, Copyright
2009, published by O'Reilly Media, Inc. ISBN 978-0-596-15713-5 covers
the use of _SESSION on pages 289-296.

Quoted text here. Click to load it

Robert Heller             -- 978-544-6933 /
Deepwoods Software        --
()  ascii ribbon campaign -- against html e-mail
/\   -- against proprietary attachments


Site Timeline