Check for the presence or expiration of a PHP session using AJAX

  • How to set php session time, I try as below, but I don't think it works

    ini_set("session.gc_maxlifetime", 600);

  • How to find out if php exists or expires using ajax (javascript)?

Hi

+5
source share
2 answers

For # 1 use session_set_cookie_params(). Expires after 600 seconds

session_set_cookie_params(600)

(note, unlike a regular setcookiefunction, it session_set_cookie_paramsuses the seconds in which you want it to live, it should not be time() + 600, which is a common mistake)

For number 2, just create a small script called via AJAX:

<?php
session_start()

if( empty($_SESSION['active']) ) {
     print "Expired"
}
else {
     print "Active"
}

?>

Javascript ( JQuery)

$.get('path/to/session_check.php', function(data) {
     if( data == "Expired" ) {
         alert("Session expired");
     } else if (data == "Active" ) {
         alert("Session active");
     }
 });
+8

Shadow Wizard , .

. AJAX , , . , - 15 , AJAX 16 .

, - - , . , .

, , !

+2

All Articles