0

设置 cookie 后,我关闭网站并重新进入它。我在浏览器选项中检查 cookie,并且我只有 sessionid 存储在我网站的 cookie 中。

这是代码:

<!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>
4

1 回答 1

1

在向客户端发送任何内容之前,您必须发送 cookie ( setcookie)(删除<!DOCTYPE html><html>顶部的)。同样适用于header命令,它也仅在任何内容之前发送时才有效。

于 2016-01-02T18:52:22.530 回答