note 104140 deleted from function.session-cache-expire by danbrown

From: Date: Wed, 25 May 2011 20:17:19 +0000
Subject: note 104140 deleted from function.session-cache-expire by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-180046@lists.php.net to get a copy of this message
Note Submitter: bluegate010 at gmail dot com ---- Here's my solution to keeping sessions from expiring: I start with a PHP file that simply calls session_start() and writes to a $_SESSION variable; doesn't matter what it's writing, could be as simple as $_SESSION['foo'] = $_SESSION['foo']; Next, I wrote a JavaScript function that sends an AJAX request to the above PHP file one minute before the session expires, repeatedly: var session_duration = <?php echo ini_get("session.gc_maxlifetime"); ?> * 1000; var session_window = 60 * 1000; setTimeout("session_refresh()", session_duration - session_window); function session_refresh() { var xmlHttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlHttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } var url = "session_refresh.php"; // <-- The name of the PHP file from above. xmlHttp.open("GET", url, true); xmlHttp.send(null); setTimeout("session_refresh()", session_duration - session_window); } This piece of JavaScript is put in a file called 'data_refresh.php' or something (must be .php for the correct session expiration time to be used). Now, in whatever page I need to keep the session active indefinitely, I just insert this line: <script type="text/javascript" src="data_refresh.php"></script> In most cases the session duration should be around half an hour, so it shouldn't put too much extra load on your server.

« previous php.notes (#180046) next »