登录时,Cookie无法正常运行

时间:2018-05-09 15:12:22

标签: session cookies login

我的checklogin.php页面包含此代码 - :

if($re=="on"){
          setcookie("username",$myusername,time()+(86400*10));
          header("location:../index.php");
          }
      else{
          session_start();
          $_SESSION['MM_Username'] = $myusername;
          $_SESSION['MM_Password'] = $mypassword; 
          header("location:../index.php");
          }

“re”是记住我的复选框。 页面index.php以 - :

开头
session_start();
if(isset($_SESSION['MM_Username'])){
     $username=$_SESSION['MM_Username'];
     $password= $_SESSION['MM_Password'];
     $sql1="SELECT * FROM users WHERE username='$username' and password='$password'";
     $result1=mysqli_query($berikane,$sql1);
     $row=mysqli_fetch_assoc($result1);
     $count1=mysqli_num_rows($result1);
     }
elseif (isset($_COOKIE['username'])){
     $username=$_COOKIE['username'];
     $sql1="SELECT * FROM users WHERE username='$username' ";
     $result1=mysqli_query($berikane,$sql1);
     $row=mysqli_fetch_assoc($result1);
     $count1=mysqli_num_rows($result1);
     }
 $myusername = $row['username'];
 $mypassword=$row['password'];
 if ($count1==0){
     header("Location: login/index.php");    
     }
else {
     }

如果我没有选中“记住我”复选框,但是当我检查它无法登录时,它正确登录

1 个答案:

答案 0 :(得分:0)

我通过添加此脚本解决了问题 - :

$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
setcookie("username",$myusername,time()+31556926, '/', $domain, false );