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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失