Jump to content


Photo

Question about magic_quotes_gpc and $_SESSION


  • Please log in to reply
3 replies to this topic

#1 ryooichi

ryooichi
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 06 April 2006 - 05:53 PM

I have a PHP application doing MySQL-based sessions on 2 different Windows 2003 Server machines running Apache2 (Win32) and one has PHP 5.0.5 and the other 5.1.2. On both, in php.ini, magic_quotes_gpc is set to off.

On the machine running PHP 5.0.5, everything is working fine. But using PHP 5.1.2, I seem to be having an issue where setting a regular scalar variable whose name happens to match a key in the $_SESSION variable, is referring to the same thing. This reminded me to check what magic_quotes_gpc was set to and confirmed that it was in fact set to off.

For example, I have a session variable stored in $_SESSION['username'] and can correctly print out its value before I set an assumingly separate variable, $username to '' (an empty string). Afterwards, when I print out the value of $_SESSION['username'] it now reflects '' (an empty string).

I.E.:
$_SESSION['username'] = 'ryooichi';
print "\$_SESSION['username'] = \"{$_SESSION['username']}\"<br>\n"; # prints: $_SESSION['username'] = "ryooichi"
$username = '';
print "\$_SESSION['username'] = \"{$_SESSION['username']}\"<br>\n"; # prints: $_SESSION['username'] = ""

So my question is: what is going on here?

Of course, my current workaround is simply to not use $username as the name for my other variable and have changed it to just $user and the conflict has now ceased. I would just like to understand it and hopefully learn how can I control this behavior.

Thank you.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 April 2006 - 06:07 PM

FYI magic_quotes_gpc doesn't affect variable names. The only thing it does affect is the value a variable contains such as if a variables value contains quotes magic_quotes_gpc eacapes any quotes.

Now I think what you meant was register_globals rather than magic_quotes_gpc. register_globals should be off on both servers.

I tried your script provded above on my local server running PHP 5.1.2 and $username doesn't affect $_SESSION['username'] at all.

You must have something else that is set which is affecting variable names.



#3 ryooichi

ryooichi
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 06 April 2006 - 06:12 PM

I was just reading my php.ini for the 43rd time today and comparing it to the one on the other machine and you are right. On the machine having problems, register_globals was on. I tested it both ways and this was the culprit. Thank you for the quick response and pointing me in the right direction.

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 April 2006 - 06:15 PM

no problem. I thought you mean register_globals. I had read your post at least 10 times too. lol




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users