[PHP & MySQL] Realtime users online script

Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Enthusiast WhiteWizz is offline
    MemberRank
    Apr 2010 Join Date
    LithuaniaLocation
    40Posts

    happy [PHP & MySQL] Realtime users online script

    Introduction
    I have been searching the internet for guide how to make a script to show how many users are connected. However, i didn't manage to find one, so i made it myself.

    I am creating this guide for people who also aren't really experienced with PHP and is searching for something like this. This script may be a mess, but it works. :)

    Creating a column in database

    You have to create a new column in table (preferably at the end of a table). I used column named "online", so i will do the same here.
    Important: Don't forget to edit your register.php and add one more ,' ' in registration code...



    Editing login.php (it is going to change online column value from 0 to 1)
    Since i think, that you already created database connection in login script, i think that this will be enough to show you. Thus, you should add it before you echo that login was successful.
    Code:
    // inserting online state
    		mysql_query("
    		UPDATE yourtable SET online='1' WHERE username='$username'"
    		) or die(mysql_error());

    Editing logout.php (from 1 to 0)
    I think that you haven't made any connections to database in this file, so i will include the whole code.
    Code:
    <?php
    
    session_start();
    //File which connects to database and selects table
    include 'db_connect.php';
    
    //Since my session is based on persons username... This code selects the user which is doing this proccess... (i think you already know that)
    $user = $_SESSION['username'];
    $query = mysql_query("SELECT * FROM yourtable WHERE username='$user'");
    $numrows = mysql_num_rows($query);
    
    // Inserting offline state
    mysql_query("
    UPDATE yourtable SET online='0' WHERE username='$user'"
    ) or die(mysql_error());
    session_destroy();
    echo "You have been logged out.";
    
    ?>

    Users_online.php script
    Checks how many rows of "online" column is set to 1 and gives the answer. Answer is the count of online (logged-in) users. :)
    Code:
    <?php
    
    session_start();
    include 'db_connect.php';
    
    $query = mysql_query("SELECT * FROM yourtable WHERE online='1'");
    $numrows = mysql_num_rows($query);
    echo $numrows;
    
    ?>

    Sorry for my bad english, if you think that it is THAT bad ;DD

    And hope this will save some time for newbies at PHP. :)
    Last edited by WhiteWizz; 13-05-12 at 01:00 AM.


  2. #2
    Developer ETTETT is offline
    MemberRank
    May 2007 Join Date
    RukongaiLocation
    662Posts

    Re: [PHP & MySQL] Realtime users online script

    And after member login to your site and presses [X] it will leave online column to 1

  3. #3
    • ♠️​ ♦️ ♣️ ​♥️ • שเ๒єtгเ๒є is offline
    MemberRank
    Mar 2012 Join Date
    917Posts

    Re: [PHP & MySQL] Realtime users online script

    dang, u rather should work with an INT column that contains the timestamp and check the online users of the last 5 minutes. on every site action the user must update the timestamp with the current timestamp in the database to tell it he/she is online (active) again. on logout the users timestamp simply can be set to 0, so it wont be tracked in any case.

    your online user php script simply counts all entries with a timestamp BIGGER than the current timestamp decreases by 300 to get all active users within the last 5 minutes.

    cheers.
    Last edited by שเ๒єtгเ๒є; 17-05-12 at 01:47 PM.

  4. #4
    Proficient Member getty is offline
    MemberRank
    Jan 2012 Join Date
    180Posts

    Re: [PHP & MySQL] Realtime users online script

    It's unnecessary to use the Sql database for this, alternatively you can use the PHP file IO function instead - store the data on a txt file.

    Spread the workload, in this instance use PHP only, reserve the SQL priority to other database related tasks.

  5. #5
    Developer ETTETT is offline
    MemberRank
    May 2007 Join Date
    RukongaiLocation
    662Posts

    Re: [PHP & MySQL] Realtime users online script

    Quote Originally Posted by VibeTribe View Post
    dang, u rather should work with an INT column that contains the timestamp and check the online users of the last 5 minutes. on every site action the user must update the timestamp with the current timestamp in the database to tell it he/she is online (active) again. on logout the users timestamp simply can be set to 0, so it wont be tracked in any case.

    your online user php script simply counts all entries with a timestamp BIGGER than the current timestamp decreases by 360 to get all active users within the last 5 minutes.

    cheers.
    I did like that. I didn't even knew how other people does it before I tried it, but it works.

    For thread.
    It can be done with $currTime = time(); and $Time = time()+300;
    $Time is posted to database and site checks:
    PHP Code:
    $q mysql_query("SELECT * FROM users WHERE online <= '$currTime'");
    echo 
    mysql_num_rows($q); 

  6. #6
    Alpha Member Justei is offline
    MemberRank
    Oct 2007 Join Date
    /f241Location
    1,904Posts

    Re: [PHP & MySQL] Realtime users online script

    Well,
    PHP User online tutorial
    Is actually a good solution to this :P...

  7. #7
    Developer ETTETT is offline
    MemberRank
    May 2007 Join Date
    RukongaiLocation
    662Posts

    Re: [PHP & MySQL] Realtime users online script

    Quote Originally Posted by Justei View Post
    Well,
    PHP User online tutorial
    Is actually a good solution to this :P...
    Yeps. Not bad at all, but I think it's much easier to explain what it need to do in theory instead of reading full already made script.
    (IMO)

  8. #8
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [PHP & MySQL] Realtime users online script

    Quote Originally Posted by getty View Post
    It's unnecessary to use the Sql database for this, alternatively you can use the PHP file IO function instead - store the data on a txt file.

    Spread the workload, in this instance use PHP only, reserve the SQL priority to other database related tasks.
    Are you sure? File I/O isn't very fast... Besides, a connection to a SQL database is likely already established if the application is using a user database in the first place.

  9. #9
    Valued Member StreetNet is offline
    MemberRank
    Jul 2012 Join Date
    112Posts

    Re: [PHP & MySQL] Realtime users online script

    share link mysql?

  10. #10
    Developer Chris is offline
    DeveloperRank
    Nov 2008 Join Date
    933Posts

    Re: [PHP & MySQL] Realtime users online script

    Do you drive a monster truck? because you're a gravedigger.

  11. #11
    Developer Macbeth is offline
    MemberRank
    Dec 2007 Join Date
    SwedenLocation
    392Posts

    Re: [PHP & MySQL] Realtime users online script

    $query = mysql_query("SELECT * FROM yourtable WHERE online='1'");
    Just a tip. When you do querys where you don't pass any variables inside of them, you should not use "", use ' instead. It's all about performance. In cases of big applications this things can do major inpact on the over-all performance.

  12. #12
    • ♠️​ ♦️ ♣️ ​♥️ • שเ๒єtгเ๒є is offline
    MemberRank
    Mar 2012 Join Date
    917Posts

    Re: [PHP & MySQL] Realtime users online script

    Quote Originally Posted by Macbeth View Post
    Just a tip. When you do querys where you don't pass any variables inside of them, you should not use "", use ' instead. It's all about performance. In cases of big applications this things can do major inpact on the over-all performance.
    there is no matter what to choose, since " or ' is the same result. tho an integer variable shouldnt have any ' in the statement + this topic is outdated, why u bump it up with such unnecessary things?

  13. #13
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [PHP & MySQL] Realtime users online script

    Quote Originally Posted by Macbeth View Post
    Just a tip. When you do querys where you don't pass any variables inside of them, you should not use "", use ' instead. It's all about performance. In cases of big applications this things can do major inpact on the over-all performance.
    Some professionals believe that premature optimization is the root of all evil. The problem with "s is they are inconsistent. Given the choice, you should ALWAYS use single quotes because of the inconsistencies found in "s.

    Quote Originally Posted by VibeTribe View Post
    there is no matter what to choose, since " or ' is the same result. tho an integer variable shouldnt have any ' in the statement + this topic is outdated, why u bump it up with such unnecessary things?
    " and ' is not the same result in PHP. Macbeth correctly notes that you can pass variables inside of double quotes, and not single quotes. Double quotes may result in silent unexpected results, making them dangerous to use. In fact, I find most features in PHP are dangerous to use due to inconsistencies, so I try very hard not to use the language.

    Mk, now my turn to post.

    I don't see what makes this Realtime... Clients are needed to display up-to-date content on-the-fly, you just supplied server-side code..
    Last edited by s-p-n; 15-09-12 at 07:22 PM.

  14. #14
    Developer Macbeth is offline
    MemberRank
    Dec 2007 Join Date
    SwedenLocation
    392Posts

    Re: [PHP & MySQL] Realtime users online script

    Quote Originally Posted by VibeTribe View Post
    there is no matter what to choose, since " or ' is the same result. tho an integer variable shouldnt have any ' in the statement + this topic is outdated, why u bump it up with such unnecessary things?
    It doesn't matter if it's outdated or not, if someone has got some valuable content to post to the thread then why shouldn't it be posted. Just like now, you learned something new with these "'s which you obviously didn't know before.

    Regarding if there's any matter or not to the use of the "'s I refer you to s-p-n's quote as there is no need to re-write it.

  15. #15
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [PHP & MySQL] Realtime users online script

    I wrote in the rules that this Tutorial's section in particular (of Coder's Paradise) is never outdated. If you find something useful to add, add it! This section is not for the thread starter, it's for passer-by's searching via google or coming here to find a specific tutorial. Most threads will go out-dated in two weeks, tutorials are always useful- but do get outdated. If a tutorial gets outdated, we should reply to improve it.



Page 1 of 2 12 LastLast

Advertisement