daled Posted August 15, 2007 Share Posted August 15, 2007 how can I use PHP to tell if a user is active or not. i'm using mysql to change 'status' field to 'logged in' when a user becomes active. but the only way to change the field to 'logged out' is by actually logging out, it doesn't change if you close the window. Same when a guest visits the site, i can make the field 'logged in', but can't change it to 'logged out'. How can i do this with PHP? Quote Link to comment Share on other sites More sharing options...
trq Posted August 15, 2007 Share Posted August 15, 2007 You also need to insert a timestamp into the db. On each request this timestamp needs to be updated. Then, you simply run a cron job every ten minutes or so that removes entries older then 10 mins or so. Quote Link to comment Share on other sites More sharing options...
daled Posted August 15, 2007 Author Share Posted August 15, 2007 alright, i can try that. what's a cron job? Quote Link to comment Share on other sites More sharing options...
chocopi Posted August 15, 2007 Share Posted August 15, 2007 http://en.wikipedia.org/wiki/Crontab Quote Link to comment Share on other sites More sharing options...
NArc0t1c Posted August 15, 2007 Share Posted August 15, 2007 Try inserting an row in the table that hold the active coloum. And then add a update query to every page(include a single file to do it) to update it to current time. And then you could use this to see if they are active: <?php $Limit = 3600; //A hour; $LastTime = mysql_query("SELECT last_time FROM table WHERE user='$user'); $Fetch = mysql_fetch_row($LastTime); if ($Fetch['last_time'] >= $Limit){ echo 'User is not active.'; } else { echo 'User Is active.'; } ?> Quote Link to comment Share on other sites More sharing options...
daled Posted August 15, 2007 Author Share Posted August 15, 2007 http://en.wikipedia.org/wiki/Crontab so then how can i make one? Quote Link to comment Share on other sites More sharing options...
trq Posted August 15, 2007 Share Posted August 15, 2007 so then how can i make one? Firstly you need to find out if you host supports it. Then, if they do, they may either provide some sort of control panel to set one up, or, better still, if you have shell access you can do it yourself. Simply run... crontab -e and add the appropriate entry. Quote Link to comment Share on other sites More sharing options...
daled Posted August 15, 2007 Author Share Posted August 15, 2007 i'm sorry that i'm such a mediocre coder, but what are you talking about? right now I don't have a host, i'm on a testing server. is that a "shell"? and then what would the 'appropriate entry' be? Quote Link to comment Share on other sites More sharing options...
trq Posted August 15, 2007 Share Posted August 15, 2007 i'm on a testing server Is the testing server Linux? The shell is a command line interface to Linux. and then what would the 'appropriate entry' be? To run a job every 10 minutes that executes a php script. */10 * * * * /path/to/php/script.php If your using windows.... I'm sorry, but you'd need to find another method (I'm not sure). Quote Link to comment Share on other sites More sharing options...
daled Posted August 15, 2007 Author Share Posted August 15, 2007 i'm on windows with apache. :edit: read the last part, god, what hopelessness. is there anything you can point me to? Quote Link to comment Share on other sites More sharing options...
trq Posted August 15, 2007 Share Posted August 15, 2007 The only way I can think of to do it in windows would be to run the query that removes older entries on each request as well. I don't think you can run scripts at timed intervals with windows. Of course this means that your if you get to the point where there is no active users, your script would still display the last active user as being logged in. this shouldn't matter though because there will be nobody around to see it. Quote Link to comment Share on other sites More sharing options...
daled Posted August 15, 2007 Author Share Posted August 15, 2007 i found a way by running the script manually in firefox. it's good enough for me right now. the hosting i'll probably use supports cron so that'll be fine. something else i found out is using the scheduled task thing. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.