我过去曾遇到过 mySQL 注入攻击的问题,因此我使用 PDO 准备语句编写了一个新的登录脚本。如果有人能注意到它并让我知道它是否足够安全以在我的网站上使用,我将不胜感激。
代码如下:
if(isset($_POST['login'])&& !empty($_POST['username']) && !empty($_POST['password']))
{
$username=$_POST['username'];
$password=sha1($_POST['password']);
$sql = "SELECT * FROM admin WHERE username ='".$username."' AND password = '".$password."'";
$result = $PDOdbh->query($sql)->fetchAll();
$check = count($result);
if($check > '0') {
$_SESSION['loggedin'] = "1";
$_SESSION['username'] = "".$username."";
header("Location: index.php");
}
else {
$_SESSION['loggedin'] = "0";
$_SESSION['username'] = "";
header ("Location: login.php?error");
}
}
if(isset($_POST['login'])&& empty($_POST['username']) && empty($_POST['password']))
{
header ("Location: login.php?missing");
}
在管理员的 index.php 页面上,我调用了以下函数:
function checkloggedin() {
if($_SESSION['loggedin'] == "0" || $_SESSION['loggedin'] !== "1" || $_SESSION['username'] == "") {
header("Location: login.php");
exit;
}
}