In PHP, why are my session variables persisted as references?

后端 未结 4 1736
醉话见心
醉话见心 2021-01-22 09:37

Here\'s the code. It\'s a simple operation to check that a session ID isn\'t being spoofed by verifying the IP address:

session_start();
$session_ip_address = $_         


        
相关标签:
4条回答
  • 2021-01-22 09:57

    If I have:

    <?php
    
    $x = 'blah';
    $_SESSION['blah'] = $x;
    
    var_dump($_SESSION);
    

    I get:

    array(1) {
      ["blah"]=>
      string(4) "blah"
    }
    

    No references in sight. PHP 5.3.2 on Ubuntu 10.04.1

    0 讨论(0)
  • 2021-01-22 10:06

    The only time your session variables are going to be references, is when you reference a session variable to another session variable (or if the original reference is still in scope).

    Per example:

    session_start();
    $x = 'foo';
    $_SESSION['x'] = &$x;
    

    This will give you:

    array(1) {
      ["x"]=>
      string(3) "foo"
    }
    

    While this:

    $x = 'foo';
    $_SESSION['x'] = $x;
    $_SESSION['y'] = &$_SESSION['x']; // reference to another $_SESSION var
    

    Or this:

    session_start();
    $x = 'foo';
    $_SESSION['x'] = $x;
    $_SESSION['y'] = &$x;
    var_dump($_SESSION); // reference still in scope
    

    Would give you:

    array(2) {
      ["x"]=>
      string(3) "foo"
      ["y"]=>
      &string(3) "foo"
    }
    

    Either way, doing this:

    session_start();
    $y = $_SESSION['y'];
    $y = 'bar';
    

    Will not modify the y session variable. In order to do that, you'd have to do:

    session_start();
    $y = &$_SESSION['y'];
    $y = 'bar';
    
    0 讨论(0)
  • 2021-01-22 10:17

    One of my customers had a very similar problem.

    Make sure your PHP configuration (PHP.ini) has register_globals Off otherwise regular variables overwrite superglobals including PHP sessions.

    0 讨论(0)
  • 2021-01-22 10:19

    it does assign by value, the reference & next to $_SESSION has nothing to do with your expression $_SESSION['x'] = $x;

    0 讨论(0)
提交回复
热议问题