简体   繁体   中英

how to delete session associated data

say i store some data associated with a session id in a database . how do i delete the data when the session is timeout in php ( i do not want to fill my file sever with all junk data. )?

is there any call back function to call in php ?

Have the user with the session report back to your database every time he makes a request. (not a new row but update his row so you wont fill your db).

Then have a cronjob or something similar clean up all sessions that have a last report timestamp older than your session timeout value.

You can write php script

<?php
session_start();
$query = "DELETE FROM session_data WHERE session_id = '{$_SESSION['session_id']}'";
// in case you want to read this from ajax as json response
echo mysq_query($query) ? "true" : "false";
session_destroy();
?>

and access this script with ajax on uload event with JQuery

$(window).unload(function() {
    $.get('destroy_session.php', null, function(data, status) {
       // ignore result
    });
});

I don't know if this qualifies as a callback per se, but maybe you could leverage the destroy handler to remove the stale database information upon explicit session termination?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM