Ani*_*cho 2 php variables cookies 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;
}
Run Code Online (Sandbox Code Playgroud)
我试图让以下内容与这些会话一起工作:
<?php
session_start();
if($_session['admin'] == true)
{
// do nothing
}else{
header( 'Location: home.html' ) ;
}
?>
Run Code Online (Sandbox Code Playgroud)
更新:
大写会话工作,但现在当我使用logout.php时,会话不会破坏
<?php
session_start();
session_destroy();
header("location: home.html");
?>
Run Code Online (Sandbox Code Playgroud)
$_session应该=> $_SESSION.
http://php.net/manual/en/reserved.variables.session.php
第一个是有效的,因为您正在设置一个"正常"变量(可用于请求).
UPDATE
要破坏会话:
<?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();
?>
Run Code Online (Sandbox Code Playgroud)
http://php.net/manual/en/function.session-destroy.php#example-4368
另外,exit();在进行重定向后,应始终使用以防止进一步执行脚本.
| 归档时间: |
|
| 查看次数: |
759 次 |
| 最近记录: |