简体   繁体   English

如何使用PHP setcookie函数设置多个cookie

[英]How to set multiple cookies with PHP setcookie function

I'm building a login page so I need to store two cookies in the client computer. 我正在构建一个登录页面,所以我需要在客户端计算机中存储两个cookie。 One is for the user name and other one is for the user id. 一个是用户名,另一个是用户ID。 I used javascript XHTTP request to send this data. 我使用javascript XHTTP请求发送此数据。

I can set the first cookie but I can't set the second. 我可以设置第一个cookie,但我不能设置第二个。 Below is my code : 以下是我的代码:

   //geting user login details

   $userDetails = json_decode($_POST["m"]);
   $userEmail =$userDetails->useremail;
   $userPassword = $userDetails->userPassword;
    //Create Connection
    $conn = new MySQLi($servername, $username, $password, $dbname);

    //Check Connection
    if ($conn->connect_error) {
  die("Connection Failed :".$conn->connect_error);
       }

     //selecting data from database

     $sql = "SELECT  email, userPassword, name, User_ID FROM User_Details" 
      ;
     $result = $conn->query($sql);


     //checking the user details

      while($details = $result->fetch_assoc() ) {
        if($userEmail==$details["email"]&&$userPassword===$details["userPassword"]) {
          session_start();

          $name = $details["name"];
          $userId = $details["User_ID"];

          $_SESSION["name"] = $name;

          $cookieName = "userName";
          $coockieUserId = "UserId";

          setcookie ($cookieName,$name, time() + 86400, "/");
          setcookie ($cookieUserId,$userId, time() + 86400, "/");
          echo  $userId; 
          break;  
     $conn->close();
    }

I can find the first cookie(user name) with Document.cookie(); 我可以使用Document.cookie();找到第一个cookie(用户名Document.cookie(); but I can't find the second one. 但我找不到第二个。 But you can see i have put a code echo $userId; 但你可以看到我已经把代码echo $userId; so in chrome debugger I can see the user Id echoed. 所以在chrome调试器中我可以看到用户Id回显。

Can you please help me to fix this? 你能帮我解决这个问题吗? Thank you. 谢谢。

The variable is undefined 变量未定义

You've defined 你定义了

 $coockieUserId = "UserId";

and are using 并正在使用

setcookie ($cookieUserId,$userId, time() + 86400, "/");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM