Hi!
Is there any way to remove a file from a directory when a session ends?
(Like when a user closes the webbrowser)
Hi!
Is there any way to remove a file from a directory when a session ends?
(Like when a user closes the webbrowser)
If you store the session details in your database, together with a timestamp for when the session was last updated, you could use this together with a cron to delete old files as required.
R.
Aha, I'll just see if I can get it to work!
Thanks
How do i create a PHP code that checks if every session in the database is active?
The thing is that I don't want to have any time limit. I just want to check if the session (called $_SESSION) is still active. If it isn't, delete the files.
SELECT * FROM table WHERE session = [?????????]
I just need help with the [????????] part.
DO you have a sessions table? If so, have a last_impression field (or similar) which stores a timestamp of when a page on the site was last accessed. If current time - timestamp is more than a certain time interval, delete it (the session). This means that your sessions need to access/get/set from the DB as opposed to using the cookie-based storage.
My table looks like this:
picname = name
thumbname = thumbname
time = the time it was uploaded
session = $_SESSION (Given a random number)
The thing is that I don't know how to make a script that checks if the sessions in the database are active or if the user has closed their browser
Firstly, $_SESSION is unlikely to contain the session id. You get the current session id from the method session_id(), which will return the current session id or create a new one if one doesn't already exist.
Did you look at the _clean() function? You would need to modify this for your needs so that you select all sessions that are older than the current timestamp as @ardav said. Hence:
function _clean($max)
{
global $_sess_db;
$old = time() - $max;
$old = mysql_real_escape_string($old);
$sql = "SELECT `id` from `sessions` WHERE `access` < '{$old}'";
$result = mysql_query($sql);
if(mysql_num_rows($result)) {
$sessionIds = array();
while(($row = mysql_fetch_assoc($result)))
$sessionIds[] = $row['id'];
$sessionIds = implode(', ', $sessionIds);
$sql = "DELETE * FROM `images` WHERE `session` IN ({$sessionIds})";
mysql_query($sql);
}
$sql = "DELETE FROM `sessions` WHERE `access` < '{$old}'";
return mysql_query($sql, $_sess_db);
}
You must have a time limit. As soon as the user has closed their browser, it sends no further information to the server, hence there is no way to tell that the session has been terminated.
Instead, you must wait an arbitrary period of time and check whether the session has been updated in time period. This period will likely be whatever lifetime you give to a session on your website. If it hasn't, it has been terminated.
Having some images sit on a server for a period of time waiting to be deleted isn't that big a deal. Unless you have failed to mention some pertinent information pertaining to your original question?
Earlier, I tried to figure out how to do it, and this is what I got:
<?php
$randomquery = mysql_query("SELECT * FROM table");
if (!mysql_num_rows($randomquery) <= 0) {
while ($row = mysql_fetch_assoc($randomquery)) {
if (mysql_real_escape_string(time() - $row[time] >= 600)) {
unlink($row[picname1]);
unlink($row[thumb1]);
mysql_query("DELETE FROM table WHERE session = '$row[session]'");
}
}
}
?>
How about that? Will it work?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.