views:

699

answers:

5

I have the following in a page e.g. /mypage?myvar=oldvalue

$_SESSION['myvar'] = $_GET['myvar'];
$myvar = 'a_new_string'

Now $_SESSION['myvar'] has the value 'a_new_string'

Is this by design?

How can I copy the value of 'myvar' rather than a reference to it?

A: 

I don't believe it's by design, but it can certainly be annoying.

dylanfm
+2  A: 

It's a feature not a bug :-)

luckily you can turn it off, set register_globals = off in your php.ini

Pat
Downvoted because it doesn't happen!
Greg
I suppose it depends on the exact version .... In the page I linked there are numerous (user) warnings about register_globals such as "If you manipulate $user you'll manipulate $_SESSION['user'] as well" which I figured is what the OP is suffering from
Pat
This sure does sound like it's related to register globals. Are you sure that you have disabled it correctly? Edit the php.ini to be sure.
troelskn
+1  A: 

I've tested this with register_globals on and off and can't reproduce it. What version of PHP are you using (I'm on 5.2.6).

Greg
register_globals is on
EoghanM
A: 

After running this:

<?php
session_start(); 
$_GET['myvar'] = ''; 
$_SESSION['myvar'] = $_GET['myvar']; 
$myvar = 'a_new_string'; 
var_dump($_SESSION); 
?>

on PHP 5.2.6 I get this:

array(1) { ["myvar"]=>  string(0) "" }
Adriano Varoli Piazza
Sorry Moranar, I forgot to specify that I've got ?myvar=oldvalue in the query string, You are right that there is no change without myvar in the query string.
EoghanM
+2  A: 

register_globals is the invention of the devil. Fortunately in PHP 6.0 it will be entirely disabled. It wasn't just a huge security problem, it makes people confuse. Please turn it off in your php.ini using register_globals = Off More information: http://us2.php.net/register_globals Also you can check the current settings with the command if (ini_get(register_globals)) echo "turn it off! :)';

Tamas Kalman
Yes this has solved it!
EoghanM
@EoghanM you could have solved it 2 hours earlier if you had read my answer ;-)
Pat
Yes, but dh2k used the phrase 'invention of the devil' which is how I feel about PHP right now :-)
EoghanM