<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>登录</title> </head> <body> <form action="{:U('Login/index')}" method="post"> username <input type="text" name="username" /> password <input type="password" name="password" /> remember <input type="checkbox" name="check" value="1" /> <input type="submit" value="Login" /> </form> </body> </html>
//登录检查
public function index() { $Key="usezan"; $info = I("post."); $user = M('user')->where(array("username"=>$info['username']))->find(); if(!$user || $user['password'] != md5($info['password'])){ $this->error("账号或者面错误..."); } //登录成功 $data['id'] = $user['id']; //用户id $data['name'] = $user['username']; //用户显示名称 //保存session session("userinfo",$data); //如果选中记住我,就将登录信息放入Cookie中 if($info['check']){ //将登录信息,存放在Cookie中 $Value = serialize($User); $Str = md5($value.$Key); setcookie('Login', $Str . $Value,time()+60*60*24*30,'/'); } $this->redirect('Index'); }
//检查登录状态
public function Index(){ $Key="usezan"; //如果Session中没有登录信息,尝试从Cookie中加载用户信息 if (!session("?userinfo")) { $Value = $_COOKIE['Login']; // 去掉魔术引号 if (get_magic_quotes_gpc()) { $Value = stripslashes($Value); } $Str = substr($Value,0,32); $Value = substr($Value,32); //校验 if (md5($Value . $Key) == $Str) { $User = unserialize($Value); $_SESSION['userinfo'] = $User; } }