doudou7361 2015-09-06 17:16
浏览 30
已采纳

php没有设置无限的cookie

Ok, so now I have this code in my login form handler .php file:

<?php
    $username = "root";
    $password = "root";
    $hostname = "localhost";
    $db = "agendadb";
    $db = new mysqli($hostname, $username, $password, $db);
  $res = $db->query("SELECT userId FROM tblUsers WHERE username='".$_POST['username']."' AND password='".md5($_POST['password'])."'");
  if($res){
    if($row =  $res->fetch_assoc()){
      setcookie('userId', strval($row['userId']), 2147483647);
      $_COOKIE['userId']= $row['userId'];
      echo 1;
    }else{
      echo -2;
    }
  }else{
    echo -1;
  }
?>

Somewhy, the cookie userId is not being set, and I can't figure out why. The script is working because it echoes the result correctly, but the cookie is not being set and no errors are thrown.

In the begining of my index page I have the following code to check if the cookie has been set

<?php
echo '<script>';

if(!isset($_COOKIE['userId'])){
  echo 'console.log(" isset");';
}
if(empty($_COOKIE['userId'])){
  echo 'console.log(" empty");';
}
if($_COOKIE['userId']==""){
  echo 'console.log(" ==");';
}
echo '</script>';
?>

and after loging in my console in index.php always shows isset empty ==

Also, I tried using ob_start(); and ob_end_flush(); in the login handler .php file, but did not work either.

Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • dousui6488 2015-09-06 17:29
    关注

    Here are a few reasons why your cookie but not be being set.

    Not accessible by the domain

    Is it being set on the same directory as this script (are both your files in the exact same directory)? If not, then you need to specify the fourth parameter of the cookie to make it available to the whole domain, as well as fixing your parameters.

    setcookie('userId', strval($row['userId']), time() + 2147483647, "/");
    

    Stale cookie or wrong sub-domain

    If your website is querying both www and non-www versions, then you may be getting different cookies because they are being treated as different sub-domains. You can change your htaccess to fix this, or you can check by going into your development console and typing document.cookie and comparing the two pages

    Cookies aren't sent until second request or cache-related

    Cookies are also not set on the first request on the script, as they only created once the request is finished. You should refresh the page again to make sure that's not happening. Your browser could also be caching and not getting the new cookie, it'd be best for you to clear all your cookies related to your test-site first before proceeding to fix your problem.

    And also, an "infinite" cookie isn't possible.

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

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测