No announcement yet.

Basic Users Online Script

  • Filter
  • Time
  • Show
Clear All
new posts

  • Basic Users Online Script

    First of all il admit i looked everywhere for a good "Users Online" script for my website. When i found my search a failure i decided i would think about it logically and then find a way to make one myself.

    I said to myself "This would be so much easier if we could rely on users to actually click the 'Log Out' button, but as they dont my first ideas not going to work"

    My first idea was to have a field in the database which when they successfully log in updates to show their online. Then when they click the "Log Out" button it updates to show their offline. As i said not all users will click the "Log Out" button and most simply close the window making this idea pointless as it wont work.

    My second idea was too have a field in the database called "Online" and call a function in every page of my logged in section of my website called "online();" This would update the online field to a value that is simply "The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)" + 60. Then on my "Users Online Page i would query the database to show all the users where the "Online" field is greater then the current Unix Epoch.

    Now i will get onto the code for you in a sec but first you will need to add a field to your "users" database named "Online". This should be a INT(20).

    Now for the code to put into the top of each page (or if you have a globals file set this as a function in there and call the function at the top of every page ).

    PHP Code:

    //All your usual file information here like you database connection and session etc.

    $session YOUR LOGIN SESSION// fill in your login session for id (or you can tweak you code here to work the query.

    $time date('U') + 60//get the time from 1st Jan 1970 + 60 seconds.

    $sql "UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$session."'";
    $res mysql_query($sql);

    Then on your "Users Online" page or wherever you want it to show whos online you use this script.

    PHP Code:

    //All your usual file information here like you database connection and session etc.

    //get the time to check the database by
    $check date('U');

    find the users who were active within the last 60 seconds
    "SELECT * FROM `users` WHERE `online` > '".$check."'";
    $res mysql_query($sql);

    //echo  out all the results
    while($row mysql_fetch_assoc($res)){ 
    $row['username']." - "// you can tweak here to add a link to the profile etc

    I do know that this script has its flaws but its simple and roughly does the job.

    Any questions or comments feel free to post below and i hope this is helpfull (and its my first tutorial so please provide constructive critisism.

    Last edited by chuckymong; 10-16-2012, 04:01 AM. Reason: Tweaked out the little mistakes

    If your thread has been solved by a member of HTMLForums or if you managed to solve the problem yourself, could you please mark the thread as Resolved.

  • #2
    you have a little bit of cleanup in your first post, the [color] tag is not working. Other than that, that is how it works. You can also leave it like that and for those users that don't logout you can have another function that is called on every page to change users to "offline" if there login time is past 30minutes or so.
    Have a Script or Snippet you want to share?

    WWW Standards: HTML 4.01,
    HTML 5, CSS2.1, CSS3, XHTML 1.0
    PHP Standards: PHP Standards