Cancel session after some time

I am creating a website for booking online tickets. In this I do the following: the user searches the bus with seat numbers. The database is updated with location numbers using temp_seat_book = 'Y'. If he orders a ticket by paying the money, his status will be updated to final_ticket_book = 'Y'. Now I want to delete the field temp_seat_book = 'Y', but final_ticket_book = 'N'. To do this, I need to delete session_ids for more than 10 minutes and final_ticket_book = 'N'. So how can I do a background job?

+5
source share
2 answers

Instead of searching for files (which is associated with a large number of I / O operations), etc. What is a session cookie: Session Cookie
It is best to store the timestamp of the "most recent activity" in the $ _SESSION variable.
And updating session data for each request (including automatic periodic ajax calls, if any).

Suppose you want to cancel a session in 10 minutes,

if (isset($_SESSION['most_recent_activity']) && 
    (time() -   $_SESSION['most_recent_activity'] > 600)) {

 //600 seconds = 10 minutes
 session_destroy();   
 session_unset();  

 }
 $_SESSION['most_recent_activity'] = time(); // the start of the session.

To avoid attacks like Session Fixing : (Session Fixing is an attack that allows an attacker to capture a real user session) session id periodically speaks for 5 minutes (I would suggest to save the regeneration time, as well as the session expiration time a little more). A more detailed list of attacks: a list of attacks .

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
    } 
else if (time() - $_SESSION['CREATED'] > 600) {
    session_regenerate_id(true);    
    $_SESSION['CREATED'] = time();  
    }

, , session.gc-maxlifetime , .

ini_set('session.gc-maxlifetime', 600)


php.ini.

session.cookie_lifetime:

session.cookie_lifetime cookie , .

, , , . session.cookie_lifetime 0 , cookie , cookie i.e, cookie .

, .

, , !: PHP 30 ?

+9

PHP . script, , 10 :

find /path/to/session/dir/* -mmin -10

grep , final_ticket_book = 'N':

grep -l 's:17:"final_ticket_book";s:1:"N";'

( -l grep, , ).

:

find /path/to/session/dir -mmin -10|xargs grep -l 's:17:"final_ticket_book";s:1:"N";'|xargs rm -f
+3

All Articles