我必须用我的 PHP 登录脚本修复一些错误,当用户使用大写字母创建帐户时,它不会让他以全小写字母登录,所以我决定修复它,但是,它不会登录在,它只是重定向到我的消息“发生意外错误”的登录。
它通过用户名检查,但不检查密码,并且它不返回密码不正确。
我尝试创建一个新帐户,它仍然进入那个错误页面,但这一次,它实际上让我登录了,但不是你已经成功登录,现有用户无法登录,而是新的我做了。
而且我确实尝试了密码重置,它有效,但不会导致成功登录消息。
我所做的另一个更改是密码的哈希,因为我使用用户名密码 Salt,并且我从更改为$hash = hash('sha256',$Username.$Password.$Salt);
,$hash = hash('sha256',strtolower($Username).$Password.$Salt);
因为当用户名是大写或小写时,它对密码检查有影响。
我的代码:
<?php
/* Mysql data */
$MysqlUsername = "root";
$MysqlPassword = "*********";
$MysqlHostname = "localhost";
$MysqlDatabase = "teamgamersnet";
$Salt = "******";
$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 Username FROM `users` WHERE `Username` = '".$sUser."'");
$CheckPass = $Sql->query("SELECT Password FROM `users` WHERE `Username` = '".$sUser."'");
$CheckActive = $Sql->query("SELECT Activated FROM `users` WHERE `Username` = '".$sUser."'");
if ($CheckUser->num_rows == 0) { header("Location: /?p=Login&wrongusr=true"); die(); }
$xCheckUser = $CheckUser->fetch_array(MYSQLI_ASSOC);
$xCheckPass = $CheckPass->fetch_array(MYSQLI_ASSOC);
$xCheckAct = $CheckActive->fetch_array(MYSQLI_ASSOC);
if ($xCheckAct['Activated'] == "false") { header("Location: /?p=Login&activate=true"); die(); }
if(strtolower($Username) == strtolower($xCheckUser['Username'])) { // Check if username match
if($hash == $xCheckPass['Password']) { // Check if password is correct
if(isset($Remember)){
setcookie ($Cookie."User", htmlspecialchars($Username));
setcookie ($Cookie."Pass", $hash);
}
else
{
setcookie ($Cookie."User", htmlspecialchars($Username), time() + 3600);
setcookie ($Cookie."Pass", $hash, time() + 3600);
}
header("Location: /?login=true"); // Send them to home page
}
else
{
header("Location: /?p=Login&wrongpwd=true"); // Send them to home page
}
header("Location: /?p=Login&unxer=true"); // Username error, Idk why
}
?>