Is it possible to save the session data for database purposes before being destroyed?
For instance, I have the data below from a session,
// start session
session_start();
// init session var
if (!isset($_SESSION['images'])) $_SESSION['images'] = array();
array
0 =>
array
'image_id' => int 1
'image_title' => string 'test 1' (length=6)
1 =>
array
'image_id' => int 2
'image_title' => string 'test 2' (length=6)
And I want to send it to database when the session is destroyed or when the browser is closed.
if(unset($_SESSION['images']))
{
// do something?
}
Is it feasible?
You should think of
session_start();as creating a single cookie on your browser with a hash that matches a session file on the server, the server will place any data into the $_SESSION whilst your “session” is active, once you close the browser that cookie is discarded from your browser, and eventually(a clean-up invoked by a cron) the session file is removed from the server.If you want to store persistent data for a time period or user then you should store it in the database, a cache file, memcache or such.