dsf1222 2018-08-03 09:13
浏览 60
已采纳

Cookie在PHP过期之前被删除

Something really weird, I had this code work before, after about 2weeks i encountered this problem. I have socket chat and on login i set a cookie for about 10 years gets expired, below is how i set it :

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

Till here it's work fine and getting set on browser and also the expire date is on 2028, So when user try to getting online first he/she will be redirected to index.php to check whether there is any cookie for him set or not in order to log him autimatically with the below code :

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');
    }

Above code will see if the user agent was TEST it will allow users to enter, else it will not. After being allowed it will see if the required sessions exist or not,if not then cookie should help and finding the token from database and set session for him again,if cookie and sessions was not set it will redirect to logout.php, But after 2 hours only this cookie Token gets removed and i don't know really why this happen, Any idea?

Note: My cookie was UserToken before after i got into this problem i changed it's name to Token

  • 写回答

1条回答 默认 最新

  • dongsimang4036 2018-08-04 15:20
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办