tailieunhanh - Giải pháp thiết kế web động với PHP - p 30

PAGES THAT REMEMBER: SIMPLE LOGIN AND MULTIPAGE FORMS session_regenerate_id(); break; } The time() function returns a current timestamp. By storing it in $_SESSION['start'], it becomes available to every page that begins with session_start(). 2. When a session times out, just dumping a user unceremoniously back at the login screen isn t very friendly, so it s a good idea to explain what s happened. In , add the code highlighted in bold to the PHP block immediately after the opening tag (around lines 22–26): $error"; } elseif (isset($_GET['expired'])) { ? Your session has expired. Please log in again. The. | PAGES THAT REMEMBER SIMPLE LOGIN AND MULTIPAGE FORMS session_regenerate_id break The time function returns a current timestamp. By storing it in _SESSION start it becomes available to every page that begins with session_start . 2. When a session times out just dumping a user unceremoniously back at the login screen isn t very friendly so it s a good idea to explain what s happened. In add the code highlighted in bold to the PHP block immediately after the opening body tag around lines 22-26 php if error echo p error p elseif isset GET expired p Your session has expired. Please log in again. p php The message is shown if the URL contains a variable called expired in a query string. 3. Open cut the code in the PHP block above the DOCTYPE declaration and paste it into a new blank file. 4. Save the file as in the includes folder and edit the code like this php session_start ob_start set a time limit in seconds timelimit 15 get the current time now time where to redirect if rejected redirect http localhost phpsols sessions if session variable not set redirect to login page if isset _SESSION authenticated header Location redirect exit elseif now _SESSION start timelimit if timelimit has expired destroy session and redirect _SESSION array invalidate the session cookie if isset _COOKIE session_name setcookie session name time -86400 end session and redirect with query string session_destroy header Location redirect expired yes 271 CHAPTER 9 Download from Wow eBook exit else if it s got this far it s OK so update start time SESSION start time The inline comments explain what is going on and you should recognize most of the elseif clause from PHP Solution 9-5. PHP measures time in seconds and I ve set timelimit in line 5 to a ridiculously short 15 seconds purely to demonstrate the effect. To set a more reasonable limit of say 15 minutes change this later like this timelimit 15 60 15 minutes You could of .

TỪ KHÓA LIÊN QUAN