這篇文章主要介紹了PHP利用Cookie設定使用者30分鐘未操作自動退出功能,需要的朋友可以參考下
登陸控制器需要做的登陸成功把用戶ID等資訊存入cookie :
$this->systemSetKey(array(‘name‘=>$admin_info[‘admin_name‘], ‘id‘=>$admin_info[‘admin_id‘],‘gid‘=>$admin_info[‘admin_gid‘],‘sp‘=>$admin_info[‘admin_is_super‘]));//登陆成功之后做得事情
父類別中的systemSetKey 方法:
/** * 系统后台 会员登录后 将会员验证内容写入对应cookie中 * * @param string $name 用户名 * @param int $id 用户ID * @return bool 布尔类型的返回结果 */ protected final function systemSetKey($user){ setNcCookie(‘sys_key‘,encrypt(serialize($user),MD5_KEY),3600,‘‘,null);//设置cookie 过期时间为30分钟。这边设置cookie框架有带自己加密规则,具体是否需要加密自己看着设置。 }
父類別控制器建構方法判斷使用者是否有登陸:
protected function __construct(){ Language::read(‘common,layout‘); /** * 验证用户是否登录 * $admin_info 管理员资料 name id */ $this->admin_info = $this->systemLogin();//取得管理员的资料,之后的子类控制器继承构造方法 if ($this->admin_info[‘id‘] != 1){ // 验证权限 $this->checkPermission(); } //转码 防止GBK下用ajax调用时传汉字数据出现乱码 if (($_GET[‘branch‘]!=‘‘ || $_GET[‘op‘]==‘ajax‘) && strtoupper(CHARSET) == ‘GBK‘){ $_GET = Language::getGBK($_GET); } } /** * 系统后台登录验证 * * @param * @return array 数组类型的返回结果 */ protected final function systemLogin(){ //取得cookie内容,解密,和系统匹配 $user = unserialize(decrypt(cookie(‘sys_key‘),MD5_KEY));//取cookie 里面储存的信息,现在使用的框架里面自定义了cookie的加密方式 if (!key_exists(‘gid‘,(array)$user) || !isset($user[‘sp‘]) || (empty($user[‘name‘]) || empty($user[‘id‘]))){ //假如不存在说明用户没登陆或者用户长时间未操作cookie时间过期 跳到登陆页面去 @header(‘Location: index.php?mod=login&action=login‘);exit; }else { $this->systemSetKey($user);//如果用户有登陆的话,每一个操作都会重写刷新cookie; } return $user; }
#加密函數:
/** * 加密函数 * * @param string $txt 需要加密的字符串 * @param string $key 密钥 * @return string 返回加密结果 */ function encrypt($txt, $key = ‘‘){ if (empty($txt)) return $txt; if (empty($key)) $key = md5(MD5_KEY); $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_."; $ikey ="-x6g6ZWm2G9g_vr0Bo.pOq3kRIxsZ6rm"; $nh1 = rand(0,64); $nh2 = rand(0,64); $nh3 = rand(0,64); $ch1 = $chars{$nh1}; $ch2 = $chars{$nh2}; $ch3 = $chars{$nh3}; $nhnum = $nh1 + $nh2 + $nh3; $knum = 0;$i = 0; while(isset($key{$i})) $knum +=ord($key{$i++}); $mdKey = substr(md5(md5(md5($key.$ch1).$ch2.$ikey).$ch3),$nhnum%8,$knum%8 + 16); $txt = base64_encode(time().‘_‘.$txt); $txt = str_replace(array(‘+‘,‘/‘,‘=‘),array(‘-‘,‘_‘,‘.‘),$txt); $tmp = ‘‘; $j=0;$k = 0; $tlen = strlen($txt); $klen = strlen($mdKey); for ($i=0; $i<$tlen; $i++) { $k = $k == $klen ? 0 : $k; $j = ($nhnum+strpos($chars,$txt{$i})+ord($mdKey{$k++}))%64; $tmp .= $chars{$j}; } $tmplen = strlen($tmp); $tmp = substr_replace($tmp,$ch3,$nh2 % ++$tmplen,0); $tmp = substr_replace($tmp,$ch2,$nh1 % ++$tmplen,0); $tmp = substr_replace($tmp,$ch1,$knum % ++$tmplen,0); return $tmp; }## 解密函數:
/** * 解密函数 * * @param string $txt 需要解密的字符串 * @param string $key 密匙 * @return string 字符串类型的返回结果 */ function decrypt($txt, $key = ‘‘, $ttl = 0){ if (empty($txt)) return $txt; if (empty($key)) $key = md5(MD5_KEY); $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_."; $ikey ="-x6g6ZWm2G9g_vr0Bo.pOq3kRIxsZ6rm"; $knum = 0;$i = 0; $tlen = @strlen($txt); while(isset($key{$i})) $knum +=ord($key{$i++}); $ch1 = @$txt{$knum % $tlen}; $nh1 = strpos($chars,$ch1); $txt = @substr_replace($txt,‘‘,$knum % $tlen--,1); $ch2 = @$txt{$nh1 % $tlen}; $nh2 = @strpos($chars,$ch2); $txt = @substr_replace($txt,‘‘,$nh1 % $tlen--,1); $ch3 = @$txt{$nh2 % $tlen}; $nh3 = @strpos($chars,$ch3); $txt = @substr_replace($txt,‘‘,$nh2 % $tlen--,1); $nhnum = $nh1 + $nh2 + $nh3; $mdKey = substr(md5(md5(md5($key.$ch1).$ch2.$ikey).$ch3),$nhnum % 8,$knum % 8 + 16); $tmp = ‘‘; $j=0; $k = 0; $tlen = @strlen($txt); $klen = @strlen($mdKey); for ($i=0; $i<$tlen; $i++) { $k = $k == $klen ? 0 : $k; $j = strpos($chars,$txt{$i})-$nhnum - ord($mdKey{$k++}); while ($j<0) $j+=64; $tmp .= $chars{$j}; } $tmp = str_replace(array(‘-‘,‘_‘,‘.‘),array(‘+‘,‘/‘,‘=‘),$tmp); $tmp = trim(base64_decode($tmp)); if (preg_match("/\d{10}_/s",substr($tmp,0,11))){ if ($ttl > 0 && (time() - substr($tmp,0,11) > $ttl)){ $tmp = null; }else{ $tmp = substr($tmp,11); } } return $tmp; }以上所述是小編給大家介紹的PHP利用Cookie設定用戶30分鐘未操作自動退出功能,希望對大家有所幫助! 相關推薦:
以上是PHP實作基於Cookie設定使用者30分鐘未操作自動退出功能的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自動化intifications andMarketingCampaigns.1)設置設置yourphpenvenvironnvironnvironmentwithaweberswithawebserverserververandphp,確保themailfunctionisenabled.2)useabasicscruct

發送電子郵件的最佳方法是使用PHPMailer庫。 1)使用mail()函數簡單但不可靠,可能導致郵件進入垃圾郵件或無法送達。 2)PHPMailer提供更好的控制和可靠性,支持HTML郵件、附件和SMTP認證。 3)確保正確配置SMTP設置並使用加密(如STARTTLS或SSL/TLS)以增強安全性。 4)對於大量郵件,考慮使用郵件隊列系統來優化性能。

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP發送郵件可以通過PHPMailer庫實現。 1)安裝並配置PHPMailer,2)設置SMTP服務器細節,3)定義郵件內容,4)發送郵件並處理錯誤。使用此方法可以確保郵件的可靠性和安全性。

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)