2016-01-02 43 views
0

在設置好cookies之後,我關閉網站並重新進入它。我在瀏覽器選項中檢查cookie,並且我只有sessionid存儲在我的網站的cookie中。php cookies不起作用

下面是代碼:

<!DOCTYPE html> 
<html> 
<?php 
    require "D:/xampp/htdocs/Form_DB/Classes/ConnectionToDB.php"; 
    require 'D:\xampp\htdocs\Form_DB\Classes\functionsMainPhp.php'; 

    session_start(); 


    if(isset($_POST['LoginSubmit'])){ 
     if(isset($_POST['LoginName']) and isset($_POST['LoginPass']) and validateNamebox($_POST['LoginName']) and validatePassword($_POST['LoginPass'])){ 
      $loginName = $_POST['LoginName']; 
      $loginPass = $_POST['LoginPass']; 
      $db = ConnectionToDB::getInstance()->getConnection(); 
      $query = $db->prepare("select count(*) as count from account where Name=? and Pass=?"); 
      $query->execute(array($loginName,$loginPass)); 
      while($rows = $query->fetch(PDO::FETCH_ASSOC)) { 
        if ($rows['count'] == "1") { 
         $_SESSION['IsSignedUp'] = true; 
         if($_SESSION['IsSignedUp'] and isset($_POST["LoginRemember"])){ 
          setcookie($CookieName, $loginName, time() + (86400 * 30), "/"); // 86400 = 1 day 
         } 
         header('Location: MainForm2LoggedIn.php'); 
        }else{ 
         $_SESSION['IsSignedUp'] = false; 
         echo("Wrong username or password"); 
        } 
      } 
     }else{ 
      echo("Invalid username or password"); 
     } 
    } 
?> 
<head> 
<link rel="stylesheet" type="text/css" href="ProjectCSS.css"> 
<title>Login Form!</title> 
</head> 

<body id = "LoginBody"> 


<form method = "post" action = "" class = "Login" > 
<input class = "Login" type = "text" name = "LoginName" placeholder = "Username" style = "height:3em ; width:26.5em"> 
<?php if(isset($_POST['LoginName']) and (!validateNamebox($_POST['LoginName']))){ 
     echo "Name must contain characters and white spaces only"; 
    } 
?> 
<br> 
<input class = "Login" type = "text" name = "LoginPass" placeholder= "Password" style = "height:3em ; width:26.5em"> 
<?php if(isset($_POST['LoginPass']) and (!validatePassword($_POST['LoginPass']))){ 
     echo "Password must contain characters and digits only"; 
    } 
?> 
<br> 
<input class = "Login" action = "" type = "submit" name = "LoginSubmit" value = "Sign In" style = "height:2.9em ; width:28.5em" > 
<br> 
<div class = "LoginRemember">Remember me: <input type = "checkbox" name="LoginRemember" ></div> 
<div class = "NoLoginGoReg">New user?: <a href="RegistrationForm.php">Sign up here!</a></div> 
</form> 


</body> 

</html> 

回答

1

必須發送cookie(setcookie)發送任何內容給客戶端(刪除<!DOCTYPE html><html>在頂部)前。同樣適用於header命令,該命令也適用於在任何內容之前發送的情況。