Detailed descriptions of php logon timeout detection function instances and php timeout instances

Source: Internet
Author: User

Detailed descriptions of php logon timeout detection function instances and php timeout instances

Php logon timeout detection instance details

Preface:

Php login timeout problem. When the user does not operate the page after a certain period of time, the user automatically exits. The principle is to use js for access judgment! The Code is as follows (take thinkphp5.0 as an example)

1. Create a logon Forum controller:

<? Phpnamespace app \ manage \ control; use \ think \ Controller; class Main extends Controller {protected $ request; public function _ initialize () {$ this-> request = \ think \ Request:: instance ();} public function login () {if ($ this-> request-> method () = "POST ") {$ data = $ this-> request-> param (); // here is the logon verification (Supplement by yourself )....... // obtain the users in the database by logging on to the submitted information and record the ID ($ id) cookie ('admin _ id', $ result ["ID"]); // cookie cache cookie ('login _ time ', Request: instance ()-> time () + 3600); // record the logon time and cache for 1 hour} return view ();} // check whether the logon times out (js call, url: http: // your domain name/manage/main/loginLosetime) public function loginLosetime () {$ logintime = cookie ('login _ Time'); $ TIME = request ()-> time (); if ($ time> $ logintime) {return json (['code' => 1, 'msg '=>' logon timeout! ', 'Url' => url ('main/login')]);} else {return json (['code' => 0]);}

2. Create a public controller (All controllers that need to verify logon inherit this controller)

<? Phpnamespace app \ common \ control; use \ think \ Controller; class AdminBase extends Controller {protected $ request; public function _ initialize () {parent: _ initialize (); $ this-> request = \ think \ Request: instance (); $ this-> checkLogin (); // detect logon $ this-> doAction (); // record action} protected function checkLogin () {$ cookie_admin_id = cookie ('admin _ id'); if (! Empty ($ cookie_admin_id) {// obtain logon user information .......} else {if ($ this-> request-> isAjax () {return $ this-> error ('You have not logged on! ', Url ('main/login');} else {header ("Location :". url ("main/login"); exit () ;}}// page operation record protected function doAction () {$ logintime = cookie ('login _ Time '); // get the cache login timeout time $ time = request ()-> time (); // current time // determine whether the current time is greater than the cache time or the timeout time is less than 60 seconds, automatically add 1 hour if ($ time> $ logintime | ($ time-$ logintime) <60) {$ newLogintime = $ logintime + 3600; cookie ('login _ time', $ newLogintime );}}}

3. js files

$. AjaxSetup ({cache: false}); $ (function () {setInterval (function () {loginLosetime () }, 360000 ); // set the loginLosetime function to be automatically executed within one hour (time can be adjusted by yourself)}); // The loginLosetime detection function loginLosetime () {$. get (AJAX_URL + 'main/loginlosetime', function (res) {if (res. code = 1) {window. location. href = res. url ;}});}

Finally, you can call the appeal js file on all pages. You do not need to call the login page!

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.