php 已弃用的 session_is_registered 的替代方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5286446/
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
Alternative for deprecated session_is_registered
提问by Kyle
session_start();
if (!session_is_registered(user)) {
header("Location: login.php");
die();
}
What is the proper way to do this since session_is_registered()
is deprecated?
由于session_is_registered()
已弃用,执行此操作的正确方法是什么?
回答by Galen
use if ( isset( $_SESSION['user'] ) ){}
用 if ( isset( $_SESSION['user'] ) ){}
回答by Vectorjohn
isset() will not do what you want all of the time. It will fail if 'user' is falsy (0, false, null, empty string). array_key_exists() will match the truth table of session_is_registered, despite what php.net says:
isset() 不会一直做你想做的事。如果 'user' 为假(0、假、空、空字符串),它将失败。尽管 php.net 说的是,array_key_exists() 将匹配 session_is_registered 的真值表:
if ( !array_key_exists( 'user', $_SESSION ) ){
/* 'user' is in the session */
}
//in other words...
if ( array_key_exists( 'user', $_SESSION ) === session_is_registered( 'user' ) ) {
/* This is always executed */
}
回答by suman kundu
In login.php page, if your username and password is matched then it would register the user by
在 login.php 页面中,如果您的用户名和密码匹配,那么它将通过以下方式注册用户
session_register("y"); //where y register the user
header("location:user.php&name=$y");
In user.php page
在 user.php 页面中
session_start();
if(!isset( $_SESSION['y'] )) // if the user is not the same user that logged in then
{ // it redirects to the login page
header("location:login.php");
}
回答by Lamy
if (empty($_SESSION['user'])){
// session user does not exist
}