Jump to content

Getting a php script to run when a session times out?


Seaholme

Recommended Posts

Hey,

 

I'm trying to create a page which will list all the users currently online, but all I can find via google are scripts to say how many are online, not who they are. I thought I would do it so that, when people log in, their 'online/offline' column in the database is changed from 0 to 1.

 

However, I don't know how to make it so that when the session times out, this column will revert back to 0.

 

Can anybody help me work out how I might be able to do this?

 

Thanks!

Link to comment
Share on other sites

I'm not sure if this would be the best way to do it, however you could add a time column in the users table. When a user logs on, or visits a page (all pages) update the table with the current time. If that time is older than 5 minutes (or what ever you choose) then set the value to 0.

 

You could improve the process a little bit more by adding a script inside crontab to run every 5 minutes to check the times. Like above, if the time is 5 minutes in the past, then set the value to 0.

Link to comment
Share on other sites

Yes, record the expiry time and the last click time in a database. Every click both values should increment. If the expiry time is passed the current time, then the user is obviously inactive.

 

Have a look through the following tutorials. You'll get the idea.

http://www.tutcity.com/tutorial/php-user-online.21756.html

Link to comment
Share on other sites

Thanks guys!

 

I decided to do what you suggested and I have a lastclick column in the database now... I'd really like to be able to write a script which would check if the lastclick was within the last 5 minutes (or actually just anything which will calculate how long it was since the last click). This is the script I'm trying to use to work out what the time difference is, but it persists in showing the difference as = 8 no matter what!

 

Can anybody help me sort it out?

// Find highest answer number. 
$sql = "SELECT * FROM players";
$result2=mysql_query($sql) or die(mysql_error());
while ($rows=mysql_fetch_array($result2)){
echo $rows['lastclick'];
}

$currentdate=date("G:i:s d/m/y");

$difference = $currentdate - $rows['lastclick'];

echo $difference;

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.