ThinkPHP3.2.2實現持久登入(記住我)功能的方法,thinkphp3.2.2
本文執行個體講述了ThinkPHP3.2.2實現持久登入功能的方法。分享給大家供大家參考,具體如下:
實現持久登入,即使用者在登入時,勾選了"記住我"之後,無論是否關閉瀏覽器,只要不退出登入,在指定的時間內始終保持登入狀態(缺點是在另一台電腦上登入過後,之前那台電腦就不能繼續保持登入狀態)。
首先,持久登陸使用 cookie 實現,但是 cookie 中不能儲存使用者密碼這樣重要的資訊,即使加密過。解決方案是在使用者登入表中建立3個欄位identifier:第二身份標識,token:永久登入標識,timeout:永久登入逾時時間。
+------------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+------------+-------------+------+-----+---------+----------------+| uid | int(11) | NO | PRI | NULL | auto_increment || uname | varchar(20) | YES | | NULL | || upwd | varchar(20) | YES | | NULL | || uflag | int(11) | YES | | NULL | || identifier | varchar(32) | YES | | NULL | || token | varchar(32) | YES | | NULL | || timeout | int(11) | YES | | NULL | |+------------+-------------+------+-----+---------+----------------+
在使用者勾選了"記住我"登入時,應該產生一個唯一的 identifier,一個唯一的 token,並且設定一個到期時間 timeout,把兩個代表身份的值寫入cookie,設定 cookie 到期時間為 timeout,例如:setcookie('auth',"$identifier:$token",$timeout); 同時把三個值插入資料表;當使用者再一次訪問網站時,首先判斷 cookie 中是否含有 auth,如果含有,則去資料庫中進行身份比對(identifier 和 token),比對成功時,把使用者資訊寫入 session,同時使用者保持登入狀態。
代碼:
控制器 TestController.class.php
<?phpnamespace Test\Controller;use Think\Controller;class TestController extends Controller { public function login(){ //判斷是否永久登入 $this->checkLong(); //已經登入則跳轉至個人中心 if(isset($_SESSION['username'])){ $this->redirect('Test/ucenter'); }else{ //判斷是否存在cookie if(isset($_COOKIE['username'])){ $this->assign('username',$_COOKIE['username']); } //顯示註冊頁 $this->display("test"); } } //顯示驗證碼 public function verifyImg(){ $verify = new \Think\Verify(); //$verify->useZh = true; //使用中文驗證碼 $verify->length = 4; $verify->entry(); } //驗證登入 public function check(){ $verify = new \Think\Verify(); if($verify->check(I("yzm"))){ //判斷使用者名稱密碼 $user = new \Test\Model\TestModel(); $res = $user->checkName(I("username"),I("pwd")); if($res === false){ echo "使用者名稱或密碼錯誤"; }else{ //使用者資訊存入session session("username",$res['uname']); session("id",$res['uid']); //如果使用者勾選了"記住我",則保持持久登陸 if(I("remember")){ $salt = $this->random_str(16); //第二分身標識 $identifier = md5($salt . md5(I("username") . $salt)); //永久登入標識 $token = md5(uniqid(rand(), true)); //永久登入逾時時間(1周) $timeout = time()+3600*24*7; //存入cookie setcookie('auth',"$identifier:$token",$timeout); $user->saveRemember($res['uid'],$identifier,$token,$timeout); } //把使用者名稱存入cookie,退出登入後在表單儲存使用者名稱資訊 setcookie('username',I('username'),time()+3600*24); //跳轉至測試人員中樞 $this->redirect('Test/ucenter'); } }else{ echo "輸入錯誤"; } } //測試strstr函數 public function strstrtest(){ $param = "Think\Verify"; //第三個參數為true,返回'Think';沒有第三個參數,返回'\Verify' $name = strstr($param,'\\',true); echo $name; } //使用者中心 public function ucenter(){ //判斷是否永久登入 $this->checkLong(); $this->assign("session",$_SESSION); $this->display("ucenter"); } //退出登入 public function loginout(){ session(null); setcookie('auth', '', time()-1); $this->redirect("Test/login"); } //產生隨機數,用於產生salt public function random_str($length){ //產生一個包含 大寫英文字母, 小寫英文字母, 數字 的數組 $arr = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z')); $str = ''; $arr_len = count($arr); for ($i = 0; $i < $length; $i++){ $rand = mt_rand(0, $arr_len-1); $str.=$arr[$rand]; } return $str; } //判斷是否持久登入 public function checkLong(){ $check = new \Test\Model\TestModel(); $is_long = $check->checkRemember(); if($is_long === false){ }else{ session("username",$is_long['uname']); session("id",$is_long['uid']); } }}
模型 TestModel.class.php
<?phpnamespace Test\Model;use Think\Model;class TestModel extends Model{ //驗證登入資訊 public function checkName($name,$pwd){ $admin = M("admin"); $info = $admin->getByUname($name); if($info != null){ //驗證密碼 if($info['upwd'] == $pwd){ return $info; }else{ return false; } }else{ return false; } } //當使用者勾選"記住我" public function saveRemember($uid,$identifier,$token,$timeout){ $admin = M("admin"); $data['identifier'] = $identifier; $data['token'] = $token; $data['timeout'] = $timeout; $where = " uid = ".$uid; $res = $admin->data($data)->where($where)->save(); return $res; } //驗證使用者是否永久登入(記住我) public function checkRemember(){ $arr = array(); $now = time(); list($identifier,$token) = explode(':',$_COOKIE['auth']); if (ctype_alnum($identifier) && ctype_alnum($token)){ $arr['identifier'] = $identifier; $arr['token'] = $token; }else{ return false; } $admin = M("admin"); $info = $admin->getByidentifier($arr['identifier']); if($info != null){ if($arr['token'] != $info['token']){ return false; }else if($now > $info['timeout']){ return false; }else{ return $info; } }else{ return false; } }}
視圖 登入頁 test.html
Document
視圖 個人中心 ucenter.html
Documenttitle></head> {$session.username}, 遊客, 歡迎您
退出登入
附:模組目錄
補充:小編在這裡推薦一款本站的php格式化美化的排版工具協助大家在以後的PHP程式設計中進行代碼排版:
php代碼線上格式化美化工具:http://tools.jb51.net/code/phpformat
更多關於thinkPHP相關內容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《ThinkPHP常用方法總結》、《smarty模板入門基礎教程》及《PHP模板技術總結》。
希望本文所述對大家基於ThinkPHP架構的PHP程式設計有所協助。
http://www.bkjia.com/PHPjc/1127892.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1127892.htmlTechArticleThinkPHP3.2.2實現持久登入(記住我)功能的方法,thinkphp3.2.2 本文執行個體講述了ThinkPHP3.2.2實現持久登入功能的方法。分享給大家供大家參考,具體...