ThinkPHP3.2.2實現(xiàn)持久登錄(記住我)功能的方法
本文實例講述了ThinkPHP3.2.2實現(xiàn)持久登錄功能的方法。分享給大家供大家參考,具體如下:
實現(xiàn)持久登錄,即用戶在登錄時,勾選了"記住我"之后,無論是否關(guān)閉瀏覽器,只要不退出登錄,在指定的時間內(nèi)始終保持登錄狀態(tài)(缺點是在另一臺電腦上登錄過后,之前那臺電腦就不能繼續(xù)保持登錄狀態(tài))。
首先,持久登陸使用 cookie 實現(xiàn),但是 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 | | +------------+-------------+------+-----+---------+----------------+
在用戶勾選了"記住我"登錄時,應(yīng)該生成一個唯一的 identifier,一個唯一的 token,并且設(shè)置一個過期時間 timeout,把兩個代表身份的值寫入cookie,設(shè)置 cookie 過期時間為 timeout,例如:setcookie('auth',"$identifier:$token",$timeout); 同時把三個值插入數(shù)據(jù)表;當用戶再一次訪問網(wǎng)站時,首先判斷 cookie 中是否含有 auth,如果含有,則去數(shù)據(jù)庫中進行身份比對(identifier 和 token),比對成功時,把用戶信息寫入 session,同時用戶保持登錄狀態(tài)。
代碼:
控制器 TestController.class.php
<?php namespace Test\Controller; use Think\Controller; class TestController extends Controller { public function login(){ //判斷是否永久登錄 $this->checkLong(); //已經(jīng)登錄則跳轉(zhuǎn)至個人中心 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); //跳轉(zhuǎn)至?xí)T中心 $this->redirect('Test/ucenter'); } }else{ echo "輸入錯誤"; } } //測試strstr函數(shù) public function strstrtest(){ $param = "Think\Verify"; //第三個參數(shù)為true,返回'Think';沒有第三個參數(shù),返回'\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"); } //生成隨機數(shù),用于生成salt public function random_str($length){ //生成一個包含 大寫英文字母, 小寫英文字母, 數(shù)字 的數(shù)組 $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
<?php namespace 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
<DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action="__CONTROLLER__/check" method="post"> <if condition="$username neq null"> <input type="text" name="username" placeholder="用戶名" value="{$username}"><br> <else /> <input type="text" name="username" placeholder="用戶名"><br> </if> <input type="password" name="pwd" placeholder="密碼"><br> <input type="text" name="yzm" placeholder="驗證碼"><img src="__CONTROLLER__/verifyImg" onClick="this.src=this.src+'?'+Math.random()"><br> <input type="checkbox" name="remember" id="remember"><label for="remember">記住我</label> <input type="submit" value="提交"> </form> </body> </html>
視圖 個人中心 ucenter.html
<DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Documenttitle> </head> <body> <if condition="$session['username'] neq null"> <i>{$session.username},</i> <else /> <i>游客,</i> </if> 歡迎您<br> <a href="__CONTROLLER__/loginout">退出登錄</a> </body> </html>
附:模塊目錄
補充:小編在這里推薦一款本站的php格式化美化的排版工具幫助大家在以后的PHP程序設(shè)計中進行代碼排版:
php代碼在線格式化美化工具:http://tools.jb51.net/code/phpformat
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《smarty模板入門基礎(chǔ)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
相關(guān)文章
ThinkPHP中__initialize()和類的構(gòu)造函數(shù)__construct()用法分析
這篇文章主要介紹了ThinkPHP中__initialize()和類的構(gòu)造函數(shù)__construct()用法,以實例形式分析了ThinkPHP中類的初始化時構(gòu)造子類的方法,是采用ThinkPHP進行面向?qū)ο蟪绦蛟O(shè)計中比較重要的概念,需要的朋友可以參考下2014-11-11php實現(xiàn)統(tǒng)計IP數(shù)及在線人數(shù)的示例代碼
這篇文章主要介紹了php實現(xiàn)統(tǒng)計IP數(shù)及在線人數(shù)的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07