heman007 Posted January 19, 2009 Share Posted January 19, 2009 Hello Alll, I have been puzzled for the past couple of days, I have got a site am creating, on the site it takes note of the number of users active online, and when user logs off or closes browser, it should register that the user is off. My problem now is when the users pc freezes, now then I can't tell if they have logged off or not, cus I need the browser active to process it. I have read about using sockets, but am not to familier with sockets, I have done a script using shell script, but it means I have to run it has a cron job every 5mins, this is not as effective as using maybe sockets, does any one have any idea how I can go about doing it. I hope my explanation is help full Thank you, Emmanuel Link to comment https://forums.phpfreaks.com/topic/141423-solved-using-sockets-in-php/ Share on other sites More sharing options...
ratcateme Posted January 19, 2009 Share Posted January 19, 2009 is your idea that you open a socket to the client and when they leave or crash the socket will get closed that is not possible with php you best way is probably a cron job or something and record in your database when they last viewed a page and if that time gets more than 5 mins old then they have left Scott. Link to comment https://forums.phpfreaks.com/topic/141423-solved-using-sockets-in-php/#findComment-740301 Share on other sites More sharing options...
heman007 Posted January 19, 2009 Author Share Posted January 19, 2009 Thanks Scott for you help, like I said in my post, I already have a con job which records in the database and it is done every 5mins, however it is not instant, and have seen it in phpbb forums so I am sure there must be a way of making it constant, so that is what am trying to find out. Emmanuel Link to comment https://forums.phpfreaks.com/topic/141423-solved-using-sockets-in-php/#findComment-740303 Share on other sites More sharing options...
ratcateme Posted January 19, 2009 Share Posted January 19, 2009 Well I think proberly the best way would be to make into one query like UPDATE SET active = 0 WHERE lastactive < ".(time()-5*60) then on every page load call that and also reset lastactive to the current time and you only need to set active when they login Scott. Link to comment https://forums.phpfreaks.com/topic/141423-solved-using-sockets-in-php/#findComment-740342 Share on other sites More sharing options...
heman007 Posted January 19, 2009 Author Share Posted January 19, 2009 Yeah that's cool, I think I have go that working fine, however, your query below is a bit confusing. Yeah 1 query is fine if you don't have mutiple users and login. I am not sure how it would work. But there must be another way of knowing when a page freezes, maybe using java, I ain't sure, but this solution still means I have to run a cron job every 5mins, it won't be automated. Link to comment https://forums.phpfreaks.com/topic/141423-solved-using-sockets-in-php/#findComment-740348 Share on other sites More sharing options...
ratcateme Posted January 19, 2009 Share Posted January 19, 2009 this is written for PHP like this mysql_query("UPDATE `users` SET `active` = 0 WHERE `active` = 1 AND `lastactive` > ".(time()-5*60)) or die(mysql_error()); the query is run on the users table that has two fields active and lastactive. active is a 1 or a 0 when they login set it to 1. lastactive is the last time they viewed a page so when somebody views a page you need to update there last active to the current time like mysql_query("UPDATE `users` SET `lastactive ` = ".time()." WHERE `userid` > ".$some_var_ containing_there_user_id) or die(mysql_error()); the query up the top looks through the whole users table to find all the users you are inactive so have not viewed a page in the last 5 minutes and sets active to 0 to get the list of active user run this query $result = mysql_query("SELECT * FROM `users` WHERE `active` = 1") or die(mysql_error()); you could validate login like this $result = mysql_query("UPDATE `user` SET `active` = 1 WHERE `username` = '".$username."' AND `password` = '".$password."'") or die(mysql_error()); if(mysql_affected_rows() > 0){ //login good } Scott. Link to comment https://forums.phpfreaks.com/topic/141423-solved-using-sockets-in-php/#findComment-740735 Share on other sites More sharing options...
heman007 Posted January 20, 2009 Author Share Posted January 20, 2009 Thanks Scott for your help it does what is needed for now Link to comment https://forums.phpfreaks.com/topic/141423-solved-using-sockets-in-php/#findComment-741165 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.