我执行以下操作来设置我的 session ,这是有效的,因为 echo 出现了。但是当我转到下一页或另一页时, session 不存在吗?我做错了什么?
$session_start();
if ($username==$dbusername&&$password==$dbpassword)
{
echo"<b>Login Successful</b><br><a href='systemadmin.html'><br>Click here to access the <strong>System Admin Page</strong></a>";
$_session['username']=$dbusername;
if($username == "admin")
{
$_session['admin'] = true;
}
我正在尝试让以下内容与这些 session 一起工作:
<?php
session_start();
if($_session['admin'] == true)
{
// do nothing
}else{
header( 'Location: home.html' ) ;
}
?>
更新:
大写 session 有效,但现在当我使用 logout.php 时 session 不会被破坏
<?php
session_start();
session_destroy();
header("location: home.html");
?>
最佳答案
$_session
应该是 => $_SESSION
。
http://php.net/manual/en/reserved.variables.session.php
第一个有效,因为您正在设置一个“正常”变量(可用于请求)。
更新
销毁 session :
<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Finally, destroy the session.
session_destroy();
?>
http://php.net/manual/en/function.session-destroy.php#example-4368
此外,您应该在执行重定向后始终使用 exit();
以防止脚本进一步执行。
关于Php session 丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10373290/