我正在努力完全理解会话,并进行了以下测试:
我的代码是:
test.php
<?
@session_start();
if(isset($_SESSION['test'])) {
echo "test success";
}
?>
当我使用插件将我的cookie手动输入浏览器时:
PHPSESSID test
它无法识别它。
$_SESSION是一个"超级全局"(随处可用)数组,它使用唯一的会话id与cookie绑定。
如果你想引用你设置的cookie值,你需要使用$_cookie超全局数组。
你可以在这里阅读更多关于超级全局的信息:http://php.net/manual/en/language.variables.superglobals.php
以及$_SESSION和$_COOKIE如何在此处工作:http://php.net/manual/en/reserved.variables.cookies.phphttp://php.net/manual/en/reserved.variables.session.php
您不能像那样使用浏览器在SESSION中设置值。PHP是唯一可以将"test"键设置为值的地方,比如true或false。
session_start();
// You could assign this based on the value of a cookie
$_SESSION['test'] = true;
if ($_SESSION['test']) {
// this is a test session
}
希望能有所帮助。
要查看cookie更改的结果,请执行:
<?
@session_start();
if(session_id() == 'test') {
echo "test success";
}
cookie包含会话ID,各个会话变量存储在服务器上,使用该ID和变量名作为密钥(默认配置使用以会话ID命名的文件)。