2

i need to perform an action when the user exits from the webpage, i figured i'd use timeout to do so, it works, but only if i refresh/reload the page, which is not what i want, i need it to work automatically, if the user is inactive for say, 5 minutes, i need a certain file, belonging to that user to get destroyed. i did the following test first to check if the timeout would work:

<?php
session_start();
//set timeout
$inactive=60;
 //check to see if timeout is set
if(isset($_SESSION['timeout'])){
 $session_life= time() - $_SESSION['timeout'];
 if($session_life > $inactive)
{session_destroy();
  echo "new message<br>";
}
}
$_SESSION['timeout']= time();
 echo "after one minute,a new message should be added.";
  ?>

it works only if i reload/refresh the page, how would i make it automatically execute the command after inactivity within the said time frame?thanks in advance.

  • Once the PHP is done executing, it is sent to the web page. Therefore, it can do no more processing. Use a client-side solution. – Waleed Khan Oct 23 '12 at 18:08
  • You can also try running CRON-job on the server to run a PHP script that will perform some inactivity checks. It should be possible when session (or some user activities on the page) is database-stored. – roomcays Oct 23 '12 at 18:16

3 Answers3

1

I think the best way would be to setup a CronJob that runs every 5 mins and removes the files.

Maybe have the sessions stored in db table using session_set_save_handler and have the cronjob check when they were last active.

Are the files you want removed connected to the user or the session?

Lex
  • 468
  • 7
  • 14
  • yes, the files are connected to the user, so to save server space, i'd need them destroyed once the user is inactive for a certain period of time, how would i go about setting up a cron job? –  Oct 24 '12 at 05:13
  • i've done some research and i've seen that cron jobs are mostly done using crontabs, which runs on linux/unix, i'm using windows, how would i make it work? i need to test it before i send it to the web host, thank you. –  Oct 24 '12 at 07:33
  • Oh ye sorry. I don't have much experiance with windows servers. I did find some articles that might help though: [Configuring cron jobs on Windows](http://drupal.org/node/31506), [Emulate cronjob on windows](http://stackoverflow.com/questions/850556/how-to-emulate-cron-jobs-on-a-windows-server) – Lex Oct 24 '12 at 18:52
  • thanks for the article lex, very helpful, one question though, since multiple users would be,logged on, can i be sure that the scheduled task running the php script would work for all of them? any ideas on how to make sure the script will execute for all the users?thanks. –  Oct 27 '12 at 09:11
  • No problem. Yep it will work for all users because the cronjob is separate from the users, meaning 1 execution per 5 minutes. The php script connected to the cronjob will loop through all the active sessions in the database and do the checking/file removing stuff. – Lex Oct 27 '12 at 10:23
  • 1
    awesome,thanks so much for your help,really,thank you, people like you make forums like these rock! –  Oct 27 '12 at 11:48
  • sorry,forgot to ask, your answer raises one more question, it says the script will have to loop through all the active sessions in the database and do the file cleaning, does that mean i have to set up a database that has tables where the session variables are stored, open it up loop through everything,checking if the user has been active and deleting the inactive ones, or will the baove code work with some modification,(adding code that unlinks the files)? –  Oct 27 '12 at 12:09
  • Hay thanks mate :D Yep, you need to create a class wrapper for [session_set_save_handler](http://nz1.php.net/session_set_save_handler) that inserts/updates a row in a table. There should be some articles out there ([example](http://www.tuxradar.com/practicalphp/10/3/7)). The cronjob will go through those db rows and do the file removing stuff. Can't really say more without knowing how the rest of your system works. – Lex Oct 29 '12 at 07:06
  • this is basically the part that needs the cron job: the user visits a page where some files of theirs(bearing a unique id number belonging to them) are created, these are log files, somewhat like chatlogs, where messages they write are sent to the created file seamlessly via ajax. i obviously need the files deleted to save on server space after a while,so i set up a variable bearing the max time, maybe an hour and then use it to compare how long the user has been on the page, if they surpass the max time, i delete their file, if not, the file lives on.won't the above code be enough? –  Oct 30 '12 at 15:30
  • forgot to mention, the script that creates the user's log file and sets the session variable to the current time is in the page the user would visit, included inside it you know but the script that would check to see if the session variable($_SESSION['timeout']) is more than the maxtime is an external file, with pretty much the above format, except that unlink($filename) would be added.is this enough info?if it is, can't i just do it like this? –  Oct 30 '12 at 15:48
  • one more thing, if all the session variables are stored in a file, won't the above commands loop through all of them(the variables) in the file and do the consequent actions? you know, user 1 has $_SESSION[]=11:00 user 2 has $_SESSION[]=11:30 and the max life time is one hour, if the time is 12:00, won't the above command loop through both variables in the tmp session file and execute the necessary commands?thanks a lot! –  Oct 30 '12 at 16:58
  • 1
    Your above code will work, but the problem is when the user just closes the window. There is a unload event but thats unreliable. You should keep the code though, since it ensures the session to last the exact amount of time (session garbage collection is more random). You need the session to have a `last_updated` column and connected to the files id. Then when the cronjob executes the php file, you cycle through the session db rows (since you don't have access to any `$_SESSION` variable) and compare the `last_updated` time... if expired then unset the file. Cheers – Lex Oct 30 '12 at 20:03
  • Also, you don't need to have the cronjob running every 5 mins... maybe once a day? – Lex Oct 30 '12 at 20:04
  • now i get it mate, oh and don't worry about the time period, i'll set it to an hour or two,but i get the database thing now, i'd write the user's login time to the database and then after an hour loop through all the values, deleting the files connected to a user who has been online for more than the maxtime. –  Oct 31 '12 at 06:14
0

I am not sure if i am giving the correct answer, but if you are asking about expiring the session automatically after a given time, why don't you consider setting

session.gc_maxlifetime

in your php.ini file, that is a far more efficient solution I guess, pardon if am getting the context wrong.

And if you want some data to be deleted, that is in file system, make use of a cron job or daemon, if you don't want a call back from client side to initiate that.

egghese
  • 2,193
  • 16
  • 26
-1

Use javascript's setInterval command to run your comand / refresh to the page with the command.

Landjea
  • 384
  • 1
  • 12
  • Why `setInterval`? And what happens if the user closes their browser window prior to the timout duration? Look at the example code, op is looking for a server-side solution. – Madbreaks Oct 23 '12 at 18:10
  • I saw his code and gave him the best solution I can see. See Waleed's comment above for why you have to use a client-side solution. He would have to use sockets, otherwise, to get a server-side solution going. Huh, downvoted for a reasonable answer? – Landjea Oct 23 '12 at 18:12