Cookie在PHP中的过期日期之前被删除

时间:2018-08-03 09:13:45

标签: php cookies

真的很奇怪,我之前有这段代码,大约2周后我遇到了这个问题。我进行了套接字聊天,并且在登录时设置了大约10年的cookie过期,以下是我的设置方法:

   if (!isset($_COOKIE['Token']))
   {
     $user_token  = $user['ID'];
     $token =  md5($user_token,false);
     setcookie("Token",$token,time() + (10 * 365 * 24 * 60 * 60));
   }

直到这里一切正常,并且可以在浏览器上进行设置,并且到期日期是2028年,所以当用户尝试首次上网时,他/她将被重定向到index.php,以检查是否有任何Cookie用于他设置还是不设置,以便使用以下代码自动登录他:

session_start();
if(apache_request_headers()['User-Agent'] !== "TEST") die('Access denied');
// $agent = apache_request_headers()['User-Agent'];
// echo put_contents_file("text.txt",$agent);



    if (!isset($_SESSION['LoggedIn_Username']) OR empty($_SESSION['LoggedIn_Username']) OR $_SESSION['LoggedIn_Username'] == NULL OR $_SESSION['LoggedIn_Username'] == "" OR !isset($_COOKIE['Token']))
    {
      if (isset($_COOKIE['Token']))
      {
        $token = $_COOKIE['Token'];
        $sql = mysqli_query($connection,"SELECT Username,ID,TOKEN FROM users WHERE TOKEN='{$token}'");
        $user = mysqli_fetch_array($sql);
        if ($user == true AND $user['TOKEN'] == $token)
        {
          $_SESSION['LoggedIn_Username'] = $user['Username'];
          $_SESSION['My_ID'] = $user['ID'];
          Header('Location: profile.php');
        }else
        {
          Header('Location: logout.php');
        }
      }else
      {
        Header('Location: logout.php');
      }
    }else
    {
      Header('Location: profile.php');
    }

以上代码将查看用户代理是否为TEST,它将允许用户输入,否则将不允许。获准后,它将查看所​​需的sessions是否存在,如果不存在,则cookie应该会帮助并从数据库中查找令牌,如果{{1} }和session均未设置,它将重定向到cookie,但是2小时后仅删除了该Cookie sessions,我真的不知道为什么会这样,知道吗?

注意:我的cookie是UserToken,在遇到此问题后我将其名称更改为logout.php

1 个答案:

答案 0 :(得分:0)

So, The problem was the connection to database, It was unsuccessful...