Page 1 of 1

session variable weirdness

Posted: Sun May 14, 2006 2:54 pm
by velo
Apache 1.3, PHP 4.4 server.

I tried to set a session variable in the following manner-

Code: Select all

$_SESSION['varName'] = $_POST['varName'];
Obviously the value fo the variable is coming from an field in an HTML form.

The $_SESSION['varName'] variable is available on the page where it was set and shows up in the superglobal $_SESSION array (I used a foreach to check every value in $_SESSION). But when I would go to another page, all of the session variable are still available and unchanged save for the one I set above. I've been using sessions for a while and have never seen this behavious before. The variable is not getting unset or reset anywhere in my code and am left wondering why both the index and value for this array entry would not persist past a single page.

I've looked in my reference books and on php.net to double check and explicitly setting the array index to a value is a valid way to set a session variable. I was able to get around the behavious by adding session_register(varName); to my session set up file but this should not be necessary.


Any idea why this would be acting so oddly?

Posted: Sun May 14, 2006 4:31 pm
by santosj
Show more code.

I have also used the same method and it has known to work just fine.

I would also check that the session_start() function is on the page of which you are losing the session data.

Posted: Mon May 15, 2006 9:39 am
by BadgerC82
Stupid question :)

Have u called session_start() on each page?

Posted: Mon May 15, 2006 10:09 am
by Bill H
I have found the the $_POST, $_SESSION and local variables sometimes "bleed into" each other. I make it a point to use different names within each array and for local variables and that solves the problem. I would be willing to bet that if you change the name of $_POST['varName'] to $_POST['newName'] the problem will go away. (Of course you will need to change the form element's name.)

Posted: Mon May 15, 2006 10:18 pm
by velo
Yup, session_start() is getting called.

Thanks, BillH, will keep that in mind for next time. I actually got it to work by adding the session_register function for the variable even though I shouldn't have "had to."

Cheers

Posted: Tue May 16, 2006 8:01 am
by BadgerC82
You may be having problems because of register globals? Check if its on in your php settings

Posted: Tue May 16, 2006 8:25 am
by velo
register_globals is on, but I was not using local variables, I was always using the full format to reference the session variable- $_SESSION['varName']

I almost wonder if this happens because, to expand on BillH's comments, when I have $_POST and $_SESSION indecis that are indentical and they are both then available as a local variable "$varName" this is why the 'bleeding' of values occurs.

Posted: Tue May 16, 2006 2:08 pm
by santosj
velo wrote:register_globals is on, but I was not using local variables, I was always using the full format to reference the session variable- $_SESSION['varName']

I almost wonder if this happens because, to expand on BillH's comments, when I have $_POST and $_SESSION indecis that are indentical and they are both then available as a local variable "$varName" this is why the 'bleeding' of values occurs.
This is true for register_globals but not for $_SESSION and $_POST. $_SESSION and $_POST and two separate and referencing both with the same key should cause no problems. If you always use $_SESSION and $_POST, then you should not have any issue, however you will need to keep from using the key value as a regular named variable since register globals will overwrite it with the values in the super globals.

Turn off register globals

If at all possible. It totally sucks... unless you are new to the language and need the 'magic' for learning.