为什么我的Cookie无法正常工作?

时间:2018-08-04 00:21:28

标签: php html mysql session cookies

我感觉好像一切都放在正确的位置,但是如果有人能发现我的错误,我的cookie似乎就不会存储了。

我可以很好地登录,只是似乎没有保存cookie,因为当我注销时,登录字段中没有任何内容。

索引页-

<form action="controller/authentication.php" method="post">
           <div class="text-danger"><?php if(isset($message)) { echo $message; } ?></div>
                <div class="form-group">  
                 <label for="login">Username</label>
                 <input name="member_name" type="text" value="<?php if(isset($_COOKIE["member_login"])) { echo $_COOKIE["member_login"]; } ?>" class="form-control" />
                </div>
                <div class="form-group">
                 <label for="password">Password</label>
                 <input name="member_password" type="password" value="<?php if(isset($_COOKIE["member_password"])) { echo $_COOKIE["member_password"]; } ?>" class="form-control" />
                </div>
                <div class="form-group">
                 <input type="checkbox" name="remember" <?php if(isset($_COOKIE["member_login"])) { ?> checked <?php } ?> />
                 <label for="remember-me">Remember me</label>
                </div>
                <div class="form-group">
                 <div><input type="submit" name="login" value="Login" class="btn btn-success"></span></div>
                </div>
                </form>

身份验证页面-

//start session management


 session_start();
//connect to the database
require('../model/connection.php');
//retrieve the functions
require('../model/functions_users.php');

//retrieve the username and password entered into the form
$name = $_POST['member_name'];
$password = $_POST['member_password'];
if(isset($_POST["login"]))
{
 if(!empty($_POST["member_name"]) && !empty($_POST["member_password"]))
 {
   //call the retrieve_salt() function
   $result = retrieve_salt($name);

   //retrieve the random salt from the database
   $salt = $result['salt'];
   //generate the hashed password with the salt value
   $password = hash('sha256', $password.$salt);

   //call the login() function
   $count = login($name, $password);

  if($count == 1)
  {
   if(!empty($_POST["remember"]))
   {
    setcookie ("member_login",$name,time()+ (10 * 365 * 24 * 60 * 60));
    setcookie ("member_password",$password,time()+ (10 * 365 * 24 * 60 * 60));
    $_SESSION["user"] = $name;
   }
   else
   {
    if(isset($_COOKIE["member_login"]))
    {
     setcookie ("member_login","");
    }
    if(isset($_COOKIE["member_password"]))
    {
     setcookie ("member_password","");
    }
   }
   header("location:../view/products.php");
  }
  else
  {
   $message = "Invalid Login";
  }
 }
 else
 {
  $message = "Both are Required Fields";
 }
}



else
{
    //if login not successful, create an error message to display on the login page
    $_SESSION['error'] = 'Incorrect username or password. Please try again.';
    //redirect to login.php
    header('location:../index.php');
    }

登录功能-

function retrieve_salt($username)
{
    global $conn;
    $sql = 'SELECT * FROM customer WHERE username = :username';
    $statement = $conn->prepare($sql);
    $statement->bindValue(':username', $username);
    $statement->execute();
    $result = $statement->fetch();
    $statement->closeCursor();
    return $result;
}

//create a function to login
function login($username, $password)
{
    global $conn;
    $sql = 'SELECT * FROM customer WHERE username = :username AND password = :password';
    $statement = $conn->prepare($sql);
    $statement->bindValue(':username', $username);
    $statement->bindValue(':password', $password);
    $statement->execute();
    $result = $statement->fetchAll();
    $statement->closeCursor();
    $count = $statement->rowCount();
    return $count;
}

编辑后添加了登录功能

1 个答案:

答案 0 :(得分:1)

所以我的问题是我正在从其他文件夹中调用文件,这些文件夹在setcookie语句中需要'/'。

所以我改变了-

setcookie ("member_login",$name,time()+ (10 * 365 * 24 * 60 * 60));

至-

setcookie ("member_login",$name,time()+ (10 * 365 * 24 * 60 * 60) , '/');

如果您无法理解为什么看不到Cookie var_dump($_COOKIE);

是个很棒的功能

希望这可以帮助某些人。