我使用 PHP 制作的当前登录脚本正在运行,但是当我尝试登录并取消选中“记住我”时,它会登录,但没有设置所需的 cookie,但是,如果我选中记住框,它会登录并设置饼干。所以问题是,是什么导致它发生?我的 PHP 代码:
<?php
/* Mysql data */
$MysqlUsername = "root";
$MysqlPassword = "";
$MysqlHostname = "localhost";
$MysqlDatabase = "teamgamersnet";
$Salt = "TGN2012";
$Username = $_POST['Username'];
$Password = $_POST['Password'];
$Remember = $_POST['Remember'];
$Cookie = "TGN";
$IP = $_SERVER['REMOTE_ADDR'];
if (empty($Username) || empty($Password)) { die("Please fill in all the fields"); }
$hash = hash('sha256',strtolower($Username).$Password.$Salt);
$Sql = new mysqli($MysqlHostname, $MysqlUsername, $MysqlPassword, $MysqlDatabase);
if ($Sql->connect_error){ echo $Sql->connect_error; }
$sUser = $Sql->real_escape_string($Username);
$sPass = $Sql->real_escape_string($Password);
$xPass = $Sql->real_escape_string($hash);
$CheckUser = $Sql->query("SELECT * FROM `users` WHERE `Username` = '".$sUser."'");;
if ($CheckUser->num_rows == 0) { header("Location: /?p=Login&wrongusr=true"); die(); }
$xCheckUser = $CheckUser->fetch_array(MYSQLI_ASSOC);
if ($xCheckUser['Activated'] == "false") { header("Location: /?p=Login&activate=true"); die(); }
if(strtolower($Username) == strtolower($xCheckUser['Username'])) {
if($hash == $xCheckUser['Password']) { // Check if password is correct
if(isset($Remember)){ // Set a looong cooke to remember
setcookie ($Cookie."User", htmlspecialchars($Username), time() + 99999999);
setcookie ($Cookie."Pass", $hash, time() + 99999999);
header("Location: /?login=true");
}
else
{ // Set a standard cookie
setcookie ($Cookie."User", htmlspecialchars($Username), time() + 3600);
setcookie ($Cookie."Pass", $hash, time() + 3600);
header("Location: /?login=true");
}
}
else
{
header("Location: /?p=Login&wrongpwd=true"); // Send them to home page
}
}
?>