我只需要调试生产php网站中的问题。这是原始开发人员假定$ cust_id = $ _SESSION ['cust_id'];的演示。是通过值传递的,但是以某种方式它是对会话对象的引用!
有人可以解释以下行为及其背后的原因,以及以后PHP开发人员可以做什么以免再次犯此错误。
代码:
<?php
session_start();
$_SESSION['cust_id'] = 1; // sets $_SESSION['cust_id'] 1 = all good
echo $_SESSION['cust_id'] . "<br>"; // returns 1 = all good
$cust_id = $_SESSION['cust_id']; // sets $cust_id 1 = all good //note this is not =&
echo $cust_id . "<br>"; // returns 1 = all good
echo $_SESSION['cust_id'] . "<br>"; // returns 1 = all good
$cust_id = 5; // He is going to use this variable for something else for a little bit. should be ok.
echo $cust_id . "<br>"; // returns 5 = all good
echo $_SESSION['cust_id'] . "<br>"; // He expected this to still hold 1,because he haden't changed the session variable at all
// returns 5!!! what the... $cust_id wasn't a reference to the session object surely? the original developer didn't expect that.
echo "phpversion: " . phpversion() . "<br>";
echo "ini_get('register_globals'): " . ini_get('register_globals') . "<br>";
输出:
1
1
1
5
5
phpversion: 5.3.29
ini_get('register_globals'): 1