{"id":58,"date":"2014-09-09T19:34:54","date_gmt":"2014-09-09T19:34:54","guid":{"rendered":"http:\/\/chamankumar.co.uk\/blog\/?p=58"},"modified":"2014-09-09T19:36:01","modified_gmt":"2014-09-09T19:36:01","slug":"php-session-timeouts","status":"publish","type":"post","link":"https:\/\/chamankumar.co.uk\/blog\/servers\/php-ini\/php-session-timeouts","title":{"rendered":"PHP session timeouts"},"content":{"rendered":"<p>See http:\/\/bytes.com\/topic\/php\/insights\/889606-setting-timeout-php-sessions<\/p>\n<p><!--?php <br ?--> \/***<br \/>\n* Starts a session with a specific timeout and a specific GC probability.<br \/>\n* @param int $timeout The number of seconds until it should time out.<br \/>\n* @param int $probability The probablity, in int percentage, that the garbage<br \/>\n* collection routine will be triggered right now.<br \/>\n* @param strint $cookie_domain The domain path for the cookie.<br \/>\n*\/<br \/>\nfunction session_start_timeout($timeout=5, $probability=100, $cookie_domain=&#8217;\/&#8217;) {<br \/>\n\/\/ Set the max lifetime<br \/>\nini_set(&#8220;session.gc_maxlifetime&#8221;, $timeout);<\/p>\n<p>\/\/ Set the session cookie to timout<br \/>\nini_set(&#8220;session.cookie_lifetime&#8221;, $timeout);<\/p>\n<p>\/\/ Change the save path. Sessions stored in teh same path<br \/>\n\/\/ all share the same lifetime; the lowest lifetime will be<br \/>\n\/\/ used for all. Therefore, for this to work, the session<br \/>\n\/\/ must be stored in a directory where only sessions sharing<br \/>\n\/\/ it&#8217;s lifetime are. Best to just dynamically create on.<br \/>\n$seperator = strstr(strtoupper(substr(PHP_OS, 0, 3)), &#8220;WIN&#8221;) ? &#8220;\\\\&#8221; : &#8220;\/&#8221;;<br \/>\n$path = ini_get(&#8220;session.save_path&#8221;) . $seperator . &#8220;session_&#8221; . $timeout . &#8220;sec&#8221;;<br \/>\nif(!file_exists($path)) {<br \/>\nif(!mkdir($path, 600)) {<br \/>\ntrigger_error(&#8220;Failed to create session save path directory &#8216;$path&#8217;. Check permissions.&#8221;, E_USER_ERROR);<br \/>\n}<br \/>\n}<br \/>\nini_set(&#8220;session.save_path&#8221;, $path);<\/p>\n<p>\/\/ Set the chance to trigger the garbage collection.<br \/>\nini_set(&#8220;session.gc_probability&#8221;, $probability);<br \/>\nini_set(&#8220;session.gc_divisor&#8221;, 100); \/\/ Should always be 100<\/p>\n<p>\/\/ Start the session!<br \/>\nsession_start();<\/p>\n<p>\/\/ Renew the time left until this session times out.<br \/>\n\/\/ If you skip this, the session will time out based<br \/>\n\/\/ on the time when it was created, rather than when<br \/>\n\/\/ it was last used.<br \/>\nif(isset($_COOKIE[session_name()])) {<br \/>\nsetcookie(session_name(), $_COOKIE[session_name()], time() + $timeout, $cookie_domain);<br \/>\n}<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>See http:\/\/bytes.com\/topic\/php\/insights\/889606-setting-timeout-php-sessions \/*** * Starts a session with a specific timeout and a specific GC probability. * @param int $timeout The number of seconds until it should time out. * @param int $probability The probablity, in int percentage, that the garbage * collection routine will be triggered right now. * @param strint $cookie_domain The domain [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[18,17],"tags":[],"_links":{"self":[{"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/posts\/58"}],"collection":[{"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":1,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":59,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/posts\/58\/revisions\/59"}],"wp:attachment":[{"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}