php登錄超時(shí)session的解決辦法:首先登錄時(shí)候用session記錄登錄時(shí)間;然后頁面打開時(shí)候判斷session如果不存在,就跳回登錄頁面;接著如果session存在,則將頁面加載時(shí)間和登錄時(shí)間對比;最后如果大于超時(shí)時(shí)間,則刪除即可。
【相關(guān)學(xué)習(xí)推薦:php編程(視頻)】
php登錄超時(shí)session的解決辦法:
php登錄超時(shí)session問題,原理是:
1、登錄時(shí)候用session記錄登錄時(shí)間
$_session['time']=time();2、頁面打開時(shí)候判斷session如果不存在,就跳回登錄頁面;如果session存在,則將頁面加載時(shí)間和登錄時(shí)間對比,如果大于超時(shí)時(shí)間,則刪除session,如果時(shí)間小于超時(shí)時(shí)間,則更新登錄時(shí)間session值!
登錄代碼:
public function login(){ if($_post){ $where['username']=$_post['username']; $where['password']=md5("php100".$_post['password']); $users=m('users')->where($where)->find(); if($users){ $_session['id']=$users['id']; $_session['user_shell']=md5($users['username'].$users['password']); $_session['time']=time(); $this->redirect('index/index'); }else{ echo "<script type='text/javascript'>alert('用戶名或密碼錯(cuò)誤');window.history.go(-1);</script>"; } }else{ $this->display(); } }公共控制器構(gòu)造函數(shù)代碼:
public function _initialize() { if(isset($_session['user_shell'])){ if(time()-$_session['time']>60){ unset($_session['user_shell']); $url=u('login/login'); header("location:$url"); }else{ $_session['time']=time(); } }else{ $url=u('login/login'); header("location:$url"); } }【相關(guān)學(xué)習(xí)推薦:php圖文教程】