BDu*_*elz 4 php security cookies login
我正在制作登录用户的登录/登出课程,根据用户的选择设置cookie.用户输入他们的电子邮件/密码并检查数据库,电子邮件/密码组合是否存在创建会话,并设置了cookie(用户ID)并且用户被重定向...然后我有一个记录用户的功能通过获取该cookie中保存的用户ID,检查该用户ID是否存在,然后再次将用户数据保存在会话中...我想知道是否有人看到任何有关此事的错误/不安全的事情.
简短的例子,我相信你们可以得到它的要点......
function login($email, $password, $remember){
// Check the database for email/password combo
if(/*user exists*/){ // if the user exists
$_SESSION = /*User data*/ // save the users data in a session
if($remember){
setcookie('user_id', /*User id*/); // save the user id in a cookie
}
header("location: index.php");// redirect
}
}
function Check_Cookie(){
if(isset($_COOKIE['user_id'])){
return $this->Log_In_ID($_COOKIE['user_id']);
}else{
return false
}
}
function Log_In_ID($id){
//Check the database if the user id exists
if(/*user exists*/){ // if the user exists
$_SESSION = /*User data*/ // save the users data in a session
header("location: index.php");// redirect
}else{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
它不是我想要问的一个详细的例子,但我相信你可以得到它的要点......有没有人看到任何可能有问题.如果你们有任何建议id喜欢听他们......还有,你们是否使用oop来记录用户或其他任何方式.
如果您的用户ID是一个序号,这是非常不安全的,因为任何人都可以根据自己的情况将他们的cookie更改为另一个合理的数字(例如,如果我的是1274,我可以尝试该范围内的其他数字)并立即恶搞那个用户.
您最好分配与该用户关联的临时ID,例如GUID.由于GUID在天文学上是独特的并且实际上是防碰撞的,因此它们几乎不可能从系统外部猜测或预测.
当用户登录时,您创建一个新的GUID并与用户一起存储:
UserID TokenID Expires
1274 {3F2504E0-4F89-11D3-9A0C-0305E82C3301} 9/25/2009 12:00:00
Run Code Online (Sandbox Code Playgroud)
当用户返回时,通过令牌查找其用户ID,确保令牌未过期并将其登录.然后更改其令牌.这可以保护您免受以下情况的影响: