Jump to content

users online script


zeusthegreat

Recommended Posts

Hopefully you use $_SESSIONS for the login. You could store their username or somekind of unique ID, probably IP address to a $_COOKIE and check it against the Database on your script and make an online column in the users table and set it to 1 if they are online and 0 if they are not. And do something like this:

 


if (isset($_COOKIE['online'])) {

    foreach($_COOKIE as $key => $value) { // key being "online" ex. $_COOKIE['online'] with their username stored

       // db connection
       $sql = "SELECT username FROM users WHERE username='$value'";
       $result = mysql_query($sql);
       $row = mysql_fetch_object($result)

       while ($row->online == 1) {

         // echo $row->username or do a count and echo the number if users online  

       }

       while ($row->online != 1) {

         // echo offline users (optional)

       }

    }

}

 

Im not sure how effective that code is and i would most definitely use mysqli if possible but that should get you headed in the right direction. You would have to run that script everytime one of your pages loads and reloads to check for online users every second. I would suggest putting the code in a function and returning $row->username or the number of users online then use javascript to delete the cookie on window.close and create it again at login. Of course at login when you create the online $_COOKIE update the online column to 1 and in the script above if the username in the cookie does not match the usernames in the table set online to equal 0. There are a few steps im leaving out probably but i think you get the idea :).

 

Also setting the time to 0 in setcookie() make it behave like a session. The cookie will delete on the browser close instead of using javascript to check the window.close.

Edited by Archadian
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...