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.

图片转代码服务由CSDN问答提供 功能建议

好的,现在我的登录表单处理程序 .php </ code>文件中包含此代码: </ p>

 &lt;?php 
 $ username =“root”; 
 $ password =“root”; 
 $ hostname =“localhost”; 
 $ db =  “agendadb”; 
 $ db = new mysqli($ hostname,$ username,$ password,$ db); 
 $ res = $ db-&gt; query(“SELECT userId FROM tblUsers WHERE username ='”。$ _ POST  ['username']。“'AND password ='”。md5($ _ POST ['password'])。“'”); 
 if($ res){
 if($ row = $ res-&gt;  fetch_assoc()){
 setcookie('userId',strval($ row ['userId']),2147483647); 
 $ _COOKIE ['userId'] = $ row ['userId']; 
 echo 1;  
} else {
 echo -2; 
} 
} else {
 echo -1; 
} 
?&gt; 
 </ code> </ pre> 
 
 

为什么,cookie userId </ code>没有被设置,我无法弄清楚原因。 该脚本正在工作,因为它正确地回应了结果,但没有设置cookie并且没有抛出错误。</ p>

在我的索引页面的开头我有以下代码来检查是否 已设置cookie </ p>

 &lt;?php 
echo'&lt; script&gt;'; 
 
if(!isset($ _ COOKIE ['userId'])){  
 echo'console.log(“isset”);'; 
} 
if(空($ _ COOKIE ['userId'])){
 echo'console.log(“empty”);'; 
  } 
if($ _ COOKIE ['userId'] ==“”){
 echo'console.log(“==”);'; 
} 
echo'&lt; / script&gt;'; 
?&gt;  ; 
 </ code> </ pre> 
 
 

index.php </ code>中登录我的控制台后,始终显示 isset empty == </ code> </ p>

另外,我尝试在登录处理程序 .php </ code>中使用 ob_start(); </ code>和 ob_end_flush(); </ code> 文件,但也没有工作。 </ p>

我们将不胜感激。</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题