Mutability and Reference for PHP5 GET Variables

I have the following on the page for example. /mypage?myvar=oldvalue

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

      

Now $_SESSION['myvar']

it matters'a_new_string'

Is it by design?

How do I copy a value 'myvar'

and not a reference to it?

0


source to share


5 answers


register_globals is an invention of the devil. Fortunately, in PHP 6.0 it will be completely disabled. This was not just a huge security issue, it made people confused. Please disable it in php.ini using register_globals = Off. More information: http://us2.php.net/register_globals You can also check the current settings with the command if (ini_get (register_globals)) echo "turn off! :) ';



+2


source


It's not a mistake: -)



Luckily you can turn it off by setting register_globals = off in php.ini

+2


source


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

+1


source


After doing 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) "" }

      

0


source


$ _ Get ['myvar'] - an element of the array $ myvar is a variable. Between these two

($ _ GET is a superglobal array)

consider the following example

$ myarry = array ('myvar' => 'myvalue'); - here myvar is an element in the $ myarray array variable $ myvar = ''; - here myvar is itself a variable.

0


source







All Articles