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 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题