PHP 替代 session_is_registered
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2600905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP alternative to session_is_registered
提问by Mauro74
does anyone knows the alternative to the deprecated function session_is_registered in PHP5?
有谁知道 PHP5 中已弃用的 session_is_registered 函数的替代方法?
here's my code:
这是我的代码:
ob_start();
session_start();
if(!session_is_registered(myusername))
{
header("location:main_login.php");
}
ob_flush();
Thanks,
谢谢,
Mauro
毛罗
回答by zaf
"You need to set and reference $_SESSION variables only." For example:
“您只需要设置和引用 $_SESSION 变量。” 例如:
if( isset($_SESSION[$myusername]) )
From http://www.phpfreaks.com/forums/index.php?topic=263189.0
回答by Sirber
on a side note, best use $_SESSION['username'] = $myusername;. Using the $_SESSION[$myusername]as a variable may overwrite existing variables in the session.
附带说明,最好使用$_SESSION['username'] = $myusername;。将$_SESSION[$myusername]用作变量可能会覆盖会话中的现有变量。
回答by kapil
But if you set anything in the session variable it will display you the protected page: e.g.,
但是如果你在 session 变量中设置了任何东西,它会向你显示受保护的页面:例如,
<?php
session_start();
if(isset($_SESSION[$myusername])){
echo "welcome to protected page.";
}
else {
header('location:login.php');
die;
}
?>
回答by RDL
Use session_id()
用 session_id()
if (!session_id()) {
// do stuff
}
if there is no session id nothing will be returned. (docs - session_id() )
如果没有会话 ID,则不会返回任何内容。(文档 - session_id() )

