Php 注销脚本在本地主机上工作,但在我的托管服务器上不工作

Php 注销脚本在本地主机上工作,但在我的托管服务器上不工作,php,session,Php,Session,最奇怪的事情发生了,当我注销我的应用程序时,它会将我重定向到正确的页面,所以脚本会运行。但是,当我随机键入一个我不应该访问的页面时,因为我的会话和cookie已被破坏,我可以访问它,这只发生在我的托管服务器上,在本地主机上工作正常,以前有人遇到过吗 启动会话脚本 <?php session_start(); // If the session vars aren't set, try to set them with a cookie if (!isset($_SESSIO

最奇怪的事情发生了,当我注销我的应用程序时,它会将我重定向到正确的页面,所以脚本会运行。但是,当我随机键入一个我不应该访问的页面时,因为我的会话和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'];
            }
       }
  ?>

在某些共享主机中,您必须包含会话目录才能工作。您确定会话已正确初始化吗

我刚刚删除了注销中删除cookie的部分,但我没有得到相同的结果。是否出于某种原因,我的共享主机帐户不允许我将cookie时间设置回两个小时以删除它们?为了简化此过程,如果我使用会话,我甚至真的需要设置cookie?无需。根据您的要求,我相信您需要会话或cookie,但两者都不需要。因此,我怀疑注销时的setcookie调用是否有效,因为它只是在setcookie调用后重新指示,要设置cookie,您需要先将一些数据传递到浏览器,然后再传递setcookie。我不是100%肯定我会看到如果我扔掉我的cookies会发生什么,我67%肯定这是我的问题……好吧……而且因为太便宜而无法使用与我写这篇文章时使用的php相同版本的主机……让这成为每个人的教训吧
<?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();
}