+ Reply to Thread
Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By phpasks

Thread: PHP - Who's Online Script Help

  1. #1
    masterjake is offline x10Hosting Member masterjake is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    73

    Question PHP - Who's Online Script Help

    Hi! I was messing around and thought of a way to make a who's online script the easy way. Basically, in my members database I have a field called "online" which is default set to 0. When they click login, if it's a success then a MySQL statement runs which sets online to 1. I also have a small script to test if anyone has online set to 1 and if so display their username to the right of the page. The only problem with this is that a lot of people never actually click the logout button and I have it set online back to 0 when they click logout. This shows them as online forever when they actually aren't.

    So, I decided to get help with an actual who's online script. Can someone please post one for me =]?

  2. #2
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: PHP - Who's Online Script Help

    Most often this is done with a field which stores the timestamp of the user's last action. In every page, a query should execute(usually from an include()'d script) which updates the last action field for the user with the time of their current action. Like this (assuming $userid holds the current user's id):

    UPDATE users SET lastaction = UNIX_TIMESTAMP() WHERE userid = '$userid'

    Then when you want to determine who's "online", you just select the users who have been active recently. For example, to get users who had been active within the last 5 minutes you could use:

    SELECT username FROM users WHERE lastaction > (UNIX_TIMESTAMP() - 300)

    MySQL timestamps are in seconds, so 300 is used for 5 minutes since it's 5 * 60.
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  3. #3
    masterjake is offline x10Hosting Member masterjake is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    73

    Re: PHP - Who's Online Script Help

    Thank you very much, I'm going to go ahead and try to set one up =]

    ADDED TO YOUR REP =]
    Last edited by masterjake; 06-14-2008 at 10:43 PM.

  4. #4
    phpasks is offline x10 Sophmore phpasks is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    145

    Re: PHP - Who's Online Script Help

    PHP Code:
    <?php
    select username from user_table 
                         where 
    (UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(current_timeslice))>='5400'
    ?>
    You can also check time, no visit then logout those member
    android99p47 likes this.
    Asif Khalyani
    http://www.phpasks.com

+ Reply to Thread

Similar Threads

  1. CRON Jobs and PHP
    By deadimp in forum Tutorials
    Replies: 14
    Last Post: 11-27-2008, 05:09 PM
  2. My first PHP script :)
    By Symbian.Ankit in forum Graphics & Webdesign
    Replies: 10
    Last Post: 02-24-2008, 01:13 AM
  3. Creating MySql database/table using php script
    By bushimports in forum Free Hosting
    Replies: 1
    Last Post: 02-02-2008, 12:43 AM
  4. How to protect images without htaccess using PHP
    By frznmnky in forum Tutorials
    Replies: 0
    Last Post: 12-26-2007, 11:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers