我对整个 PHP/SQLite 安全游戏非常陌生;我不知道如何防范注入攻击、基于 PHP_SELF 的威胁等。有人可以查看我的 55 行登录页面并指出我可能存在的任何安全漏洞吗?我真的很感激。
<?php
// Begin Redirector ******************************************************************************************
if (isset($_COOKIE["session"])) { header("location:http://pagesnap.tk/"); }
// End Redirector ******************************************************************************************
else
{
?>
<html>
<head>
<title>Sign In - PageSnap</title>
</head>
<body>
<h1>PageSnap</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="username" type="text" placeholder="Username "/>
<br />
<input name="password" type="password" placeholder="Password" />
<br />
<br />
<input name="signin" type="submit" value="Sign In" />
</form>
</body>
</html>
<?php
if ($_POST["signin"]) // If Form Is Submitted
{
// Variables
$username = $_POST["username"];
$password = $_POST["password"];
$password = hash("sha512", $password);
// Database Connection
$database = new PDO("sqlite:database.sqlite");
// Entry Finder
$result = $database -> query("SELECT COUNT (*) FROM accounts WHERE username = '$username' AND password = '$password'");
// Success
if ($result -> fetchColumn() > 0)
{
$id = $database -> query("SELECT id FROM accounts WHERE username = '$username' AND password = '$password'");
$id = $id -> fetchColumn();
setcookie("session", "$id", time()+3600);
header("location:http://pagesnap.tk/");
}
// Failure (Entry Not Found)
else
{
echo "You have entered your username or password incorrectly.";
}
}
}
?>
非常感谢!