我有以下代码,当用户登录时,会显示两个文本框和一个复选框。
设置三个cookie,username
,password
和remember
一切正常,登录脚本本身就很好了(我很欣赏在cookie中存储哈希密码并不是最好的练习,但现在它会这样做。
但是,当用户已经登录时,用户重新访问login.php
页面(这一个)的ID将被逐个删除,因为$ _POST条件未得到满足,因此下面的行正在执行。我怎样才能防止这种情况发生。此外,任何清理代码的建议,我将无疑会以大量的重复代码结束,我们将不胜感激。感谢
snippet from the 'login.php' page below
} elseif (!$_POST['remember']) {
$past = time() - 100;
if (isset($_COOKIE['remember'])) {
setcookie('remember', '', $past);
} elseif (isset($_COOKIE['username'])) {
setcookie('username', '', $past);
} elseif (isset($_COOKIE['password'])) {
setcookie('password', '', $past);
}
}
login.php
<?php
session_start();
include("includes/config.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
$odb = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
$username = "";
$password = "";
if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
$username = $_COOKIE['username'];
$password = $_COOKIE['password'];
} elseif (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$password = md5(DB_SALT.$password);
}
$sql = "SELECT * from tblMembers WHERE username = :username";
$query = $odb->prepare($sql);
$query->execute(array(":username" => $username));
$results = $query->fetchAll();
if($results !== FALSE && $query->rowCount()>0) {
if($results[0]['passwordHash'] == $password) {
$_SESSION['username'] = $username;
$_SESSION['userID'] = $results[0]['userID'];
if($_POST['remember']) {
$month = time() + (60 * 60 * 24 * 30);
setcookie('remember', $_POST['username'], $month);
setcookie('username', $_POST['username'], $month);
setcookie('password', $results[0]['passwordHash'], $month);
} elseif (!$_POST['remember']) {
$past = time() - 100;
if (isset($_COOKIE['remember'])) {
setcookie('remember', '', $past);
} elseif (isset($_COOKIE['username'])) {
setcookie('username', '', $past);
} elseif (isset($_COOKIE['password'])) {
setcookie('password', '', $past);
}
}
header("Location: "."index.php");
} else {
echo "password incorrect";
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
Username:
<?php
if(isset($_COOKIE['username'])) {
echo "<input type=\"text\" id=\"username\" name=\"username\" maxlength=\"40\" value=".$_COOKIE['username'].">";
} else {
echo "<input type=\"text\" id=\"username\" name=\"username\" maxlength=\"40\" value=\"\">";
}
?>
Password: <input type="password" id="password" name="password" maxlength="50">
Remember Me:
<?php
if(isset($_COOKIE['remember'])) {
echo "<input type=\"checkbox\" id=\"remember\" name=\"remember\" checked=\"checked\">";
} else {
echo "<input type=\"checkbox\" id=\"remember\" name=\"remember\">";
}
?>
<input type="submit" id="submit" name="submit" value="Log In">
</form>
</body>
</html>
答案 0 :(得分:2)
您的代码确切地说这应该发生。在第一次请求之后,将不再设置remember
POST参数,因此if语句的计算结果为true
。然后它将删除第一个cookie。下次它会删除第二个,因为第一个已经不存在了。
也许你应该替换这一行:
elseif (!$_POST['remember']) {
用这个:
elseif (!$_POST['remember'] && !$_COOKIE['remember']) {
你应该摆脱那里的elseif
,因为你可能想要删除所有的cookie。只需添加if
,就可以按照自己的意愿进行操作。
旁注:!$var
不是检查值是否设置的正确方法。请改用!isset($var)
。