php - Cleaning expired session from database -


i storing sessions inside mysql database see lot of sessions date of expiry has reached .now don't have other kind of script running collecting expired sessions read garbage collection there less chance garbage collection take care of large amount of expired sessions expire.

i looking forward script or alternative solution automatically deletes expired sessions?

you'll have implement session handler class, , it's gc function can delete old session data:

function gc($lifetime) {     $db = new pdo("mysql:host=myhost;dbname=mydb", "myuser", "mypassword");      $sql = "delete session session_lastaccesstime < date_sub(now(), interval " . $lifetime . " second)";     $db->query($sql); } 

garbage collection performed on random basis php. probability garbage collection invoked decided through php.ini directives session.gc_probability , session.gc_divisor. if probability set 1 , divisor set 100 example, garbage collector has 1% chance of being run on each request (1/100).

you need set gc handler:

session_set_save_handler(array($sessionhandler,"open"),                         array($sessionhandler,"close"),                         array($sessionhandler,"read"),                         array($sessionhandler,"write"),                         array($sessionhandler,"destroy"),                         array($sessionhandler,"gc")); 

see more how use it:

http://php.net/manual/en/function.session-set-save-handler.php


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -