Jump to content

check if a person is online


mingger

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

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}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.