Home  >  Article  >  php教程  >  ThinkPHP后台检测用户登录是否超时

ThinkPHP后台检测用户登录是否超时

WBOY
WBOYOriginal
2016-06-07 11:44:30777browse

由于每个操作的地方都需要先判断一下登录是否超时,就想到用构造函数,但是每个类都写一个构造函数又不方便,所以写了一个公共类,这个公共类里面还可以写许多其他的通用方法,如:_initialize 方法。

新建一个公共 Action 叫 CommAction.class.php
代码如下:class CommAction extends Action {<br> public function __construct() {<br> parent::__construct();<br> $this->checkAdminSession();<br> }<br> public function checkAdminSession() {<br> //设置超时为10分<br> $nowtime = time();<br> $s_time = $_SESSION['logintime'];<br> if (($nowtime - $s_time) > 600) {<br> unset($_SESSION['logintime']);<br> $this->error('当前用户未登录或登录超时,请重新登录', U('login/loginpage'));<br> } else {<br> $_SESSION['logintime'] = $nowtime;<br> }<br> }<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn