mingger Posted May 13, 2012 Share Posted May 13, 2012 Hey I hope this is the right place to put this question.... I'm trying to make a social network(Just to test what i can do), and i wanted to make a feature so that u could check if a person is online or not. So the person could start a chat session with a user, i have no idea how u could make this. So i hope that there is some genius that will help me with this. Thank you - Nicolaj Quote Link to comment https://forums.phpfreaks.com/topic/262484-check-if-a-person-is-online/ Share on other sites More sharing options...
doddsey_65 Posted May 13, 2012 Share Posted May 13, 2012 Well the simplest way would be to store the last activity of a user in the database. Then you would check to see if they were active within the last 5 minutes. If they were then you count them as actively online. This is how scripts like phpbb do it. You're never going to be able to check in realtime without the help of something like mongodb or nodejs, but the 5 minute rule is usually effective. Quote Link to comment https://forums.phpfreaks.com/topic/262484-check-if-a-person-is-online/#findComment-1345177 Share on other sites More sharing options...
The Letter E Posted May 14, 2012 Share Posted May 14, 2012 Hey I hope this is the right place to put this question.... I'm trying to make a social network(Just to test what i can do), and i wanted to make a feature so that u could check if a person is online or not. So the person could start a chat session with a user, i have no idea how u could make this. So i hope that there is some genius that will help me with this. Thank you - Nicolaj This problem can be more seamlessly handled using javascript. <script> function areYouThere(expires){ //fire an ajax request to update your database with a logged in status } setInterval(areYouThere(10000), 5000); </script> Then all you need to do is check the logged in status time against the expires time and you know if anybody's home. Quote Link to comment https://forums.phpfreaks.com/topic/262484-check-if-a-person-is-online/#findComment-1345207 Share on other sites More sharing options...
mingger Posted May 14, 2012 Author Share Posted May 14, 2012 Hello Thank you for the help, but im not very talented in javascript and ajax yet. So if you could write out the whole code for this, i would be so blessed Quote Link to comment https://forums.phpfreaks.com/topic/262484-check-if-a-person-is-online/#findComment-1345256 Share on other sites More sharing options...
The Letter E Posted May 14, 2012 Share Posted May 14, 2012 Hello Thank you for the help, but im not very talented in javascript and ajax yet. So if you could write out the whole code for this, i would be so blessed This is using jQuery: http://jquery.com/ This code can go anywhere in your header or footer <script> $(document).ready(function(){ function areYouThere(expires){ var url = 'path/to/the/update/script.php'; $.post(url, {loggedin: true, expires: expires}, function(data){ alert(data.result); //comment this line out once you are done testing and the back end is working - you don't need any callback in this silent function }, 'JSON'); } setInterval(areYouThere(10000), 5000); }); </script> Then all you need is the corresponding php file for the ajax request to .post to yourFile.php <?php if( $_POST['loggedin'] ){ $expires = $_POST['expires']; //connect to mysql //run the query to update your database //then you can output a json encoded array with the result if( $result = mysql_query($sql) ){ $output = array('result' => true); }else{ $output = array('result' => false); } }else{ $ouptut = array('result' => false); } print json_encode($output); ?> The theory behind the sql is that you have a field in your users table, you can call it whatever you want, maybe ping_time, or is_available, maybe a binary field, then you can store something like, 'micro time:expires', then every time you load a page that shows a users status you only need to do some quick math to see if they are currently available, if( (db_microtime + db_expires) > current_microtime ){ THEY ARE AVAILABLE...OR MAybE THEy JUST LOGGED OUT BUT THE EXPIRES TIME HASN'T LAPSED YET} Quote Link to comment https://forums.phpfreaks.com/topic/262484-check-if-a-person-is-online/#findComment-1345360 Share on other sites More sharing options...
mingger Posted May 14, 2012 Author Share Posted May 14, 2012 Thank you very much for the help! I think that i can get it to work now Quote Link to comment https://forums.phpfreaks.com/topic/262484-check-if-a-person-is-online/#findComment-1345367 Share on other sites More sharing options...
doddsey_65 Posted May 14, 2012 Share Posted May 14, 2012 An HTTP request every 5 seconds is not a good idea. Assume 10 people visit your site. Thats 10 requests. Now assume 100 people and you are updating the database for each one. Thats 100 HTTP requests and 100 queries. Using my method you only have to update the database every 5 minutes or more depending on page loads. Quote Link to comment https://forums.phpfreaks.com/topic/262484-check-if-a-person-is-online/#findComment-1345433 Share on other sites More sharing options...
The Letter E Posted May 14, 2012 Share Posted May 14, 2012 An HTTP request every 5 seconds is not a good idea. Assume 10 people visit your site. Thats 10 requests. Now assume 100 people and you are updating the database for each one. Thats 100 HTTP requests and 100 queries. Using my method you only have to update the database every 5 minutes or more depending on page loads. That's true, but if someone stays on a page long enough that method does not work well. You can also change the setInterval timing on the JS to be 5 minutes if you want. Not much difference. Quote Link to comment https://forums.phpfreaks.com/topic/262484-check-if-a-person-is-online/#findComment-1345476 Share on other sites More sharing options...
mingger Posted May 15, 2012 Author Share Posted May 15, 2012 Hey I have a question (: when the user just closes the window without logging out, how will it be able to change the row of the database? Quote Link to comment https://forums.phpfreaks.com/topic/262484-check-if-a-person-is-online/#findComment-1345529 Share on other sites More sharing options...
ajrockr Posted May 15, 2012 Share Posted May 15, 2012 It wont, but it wont have to because the last time the database will update for that person is when they loaded the last page, and that is the time you are comparing to NOW. So if their last page load time is X minutes earlier than NOW, you can assume they are offline. Quote Link to comment https://forums.phpfreaks.com/topic/262484-check-if-a-person-is-online/#findComment-1345561 Share on other sites More sharing options...
The Letter E Posted May 15, 2012 Share Posted May 15, 2012 Hey I have a question (: when the user just closes the window without logging out, how will it be able to change the row of the database? Yea, that's the point of having an expires time. Quote Link to comment https://forums.phpfreaks.com/topic/262484-check-if-a-person-is-online/#findComment-1345652 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.