最奇怪的事情正在发生,当我退出我的应用程序时,它将我重定向到正确的页面,因此脚本运行。然而,当我随机输入一个我无法访问的页面,因为我的会话和cookie已被销毁,我可以访问它,这只发生在我的托管服务器上,在本地主机上它工作正常,有没有人遇到过这个?
启动会话脚本
<?php
session_start();
// If the session vars aren't set, try to set them with a cookie
if (!isset($_SESSION['user_id'])) {
if (isset($_COOKIE['user_id']) && isset($_COOKIE['user_email'])) {
$_SESSION['user_id'] = $_COOKIE['user_id'];
$_SESSION['user_email'] = $_COOKIE['user_email'];
$_SESSION['lawyer_client'] = $_COOKIE['lawyer_client'];
}
}
?>
注销脚本
<?php
// If the user is logged in, delete the session vars to log them out
session_start();
if (isset($_SESSION['user_id'])) {
// Delete the session vars by clearing the $_SESSION array
$_SESSION = array();
// Delete the session cookie by setting its expiration to an hour ago (3600)
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time() - 7600);
}
// Destroy the session
session_unset();
session_destroy();
// Delete the user ID and username cookies by setting their expirations to an hour ago (3600)
setcookie('user_id', '', time() - 7600);
setcookie('user_email', '', time() - 7600);
setcookie('lawyer_client', '', time() - 7600);
// Redirect to the home page
$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php';
header('Location: ' . $home_url);}
?>
我正在检查会话是否使用此脚本设置
require_once('startsession.php');
if (!isset($_SESSION['user_id'])) {
echo '<p class="login">Please <a href="main_login.php">log in</a> to access this page.</p>';
exit();
}
因此,在查看我刚刚放下的内容后,我的第一个猜测就是我的注销脚本没有正确清除我的会话......但为什么它只是不在我的共享主机上呢?
答案 0 :(得分:0)
在某些共享主机中,您必须包含会话目录才能工作。您确定会话已正确初始化吗?