在下面的代码中,如果用户提供的用户名和密码是正确的,则user_id和用户名存储在会话中以保持日志记录。我的问题是,为什么需要在会话中保留user_id?不只是一件足以在会话中存储的东西(例如用户名)吗?如果启用了“记住”,则只使用用户名设置cookie。现在我的问题是,仅仅用户名cookie就够了吗?不能只在浏览器中编辑或添加cookie并登录到系统中吗?
谢谢你的回复。
<?
public function login($username, $pass, $remember) {
// check username and password with db
$result = $conn->query("select * from login where
username='".$username."' and
password=sha1('".$pass."')");
if (!$result) {
throw new depException('Incorrect username and password combination. Please try again.');
}
if ($result->num_rows>0) {
$row = $result->fetch_assoc();
$_SESSION['user_id'] = $row[user_id];
$_SESSION['username'] = $username;
// start rememberMe
$cookie_name = 'db_auth';
$cookie_time = (3600 * 24 * 30);*/ // 30 days
// check to see if user checked box
if ($remember) {
setcookie ($cookie_name, 'username='.$username, time()+$cookie_time);
}
// If all goes well redirect user to their homepage.
header('Location: http://localhost/v6/home/index.php');
} else {
throw new depException('Could not log you in.');
}
}
?>发布于 2011-03-08 18:20:40
--这段代码不是安全的!(对不起大写,但它的重点是)。SQL语句容易受到SQL注入的影响。另外,将用户名存储在cookie中也是个坏主意,因为任何人都可以伪造cookie以获得身份验证。
发布于 2011-03-08 18:20:28
如果这是安全的,我对这个问题的回答是否定的。
你需要净化你的代码。如果有人输入'test或1=1‘作为用户名会发生什么?
发布于 2011-03-08 18:26:02
我真的不知道从哪里开始。这段代码真的不安全。
您应该使用instead.
$remember是一个布尔值或整数。
sha1有点坏,所以我建议用户可以重写使用sha1,可以将username=admin添加到cookie中并以管理员身份登录。H 211f 212https://stackoverflow.com/questions/5236584
复制相似问题