note 104140 added to function.session-cache-expire

From: Date: Wed, 25 May 2011 19:46:37 +0000
Subject: note 104140 added to function.session-cache-expire
Groups: php.notes 
Request: Send a blank email to php-notes+get-180040@lists.php.net to get a copy of this message
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. ---- Server IP: 192.41.42.26 Probable Submitter: 97.126.247.179 ---- Manual Page -- http://www.php.net/manual/en/function.session-cache-expire.php Edit -- https://master.php.net/note/edit/104140 Del: integrated -- https://master.php.net/note/delete/104140/integrated Del: useless -- https://master.php.net/note/delete/104140/useless Del: bad code -- https://master.php.net/note/delete/104140/bad+code Del: spam -- https://master.php.net/note/delete/104140/spam Del: non-english -- https://master.php.net/note/delete/104140/non-english Del: in docs -- https://master.php.net/note/delete/104140/in+docs Del: other reasons-- https://master.php.net/note/delete/104140 Reject -- https://master.php.net/note/reject/104140 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#180040) next »