Displaying Users Online on your site.

Discussion in 'PHP' started by aditya_sfs, Dec 25, 2006.

  1. #1
    I just coded a small script to display the number of users currently online on your site. SO i thought to share it here. Here it goes. Create a small table in any database you already have or you may create a new database also


    Now change the table, database name, database password and host name etc. in the following code and place it wherever you want the "XX Visitors Online" message to appear in the script. Infact, for good results it must execute every time a page on the site is loaded. So better place in the overall footer of your site.

    
    <?
    $server = "localhost";
    $table = "visitors";
    $database = "xxxx";
    $username = "xxxx";
    $password = "xxxx";
    $connection = mysql_connect($server,$username,$password);
    mysql_select_db($database,$connection);
    $minutes = 5;
    $seconds = $minutes*60;
    $past = time()-$seconds;
    $now = time();
    if ( mysql_num_rows(mysql_query("SELECT * FROM $table WHERE ip='$_SERVER[REMOTE_ADDR]'")) != 0 )
    {
    	mysql_query("UPDATE $table SET date='$now' WHERE ip='$_SERVER[REMOTE_ADDR]'") or die ("Error - Unable to update visit");
    }
    else
    {
    	mysql_query("INSERT INTO $table VALUES ('$_SERVER[REMOTE_ADDR]','$now')") or die ("Error - Unable to insert records");
    }
    mysql_query("DELETE FROM $table WHERE date < $past") or die ("Error - Unable to delete old visits");
    $visitors = mysql_result(mysql_query("SELECT COUNT(*) FROM $table"),0);
    php echo ("<b>" . $visitors . "</b> guests online");
    ?>
    
    PHP:
    And you are done ;)
     
    aditya_sfs, Dec 25, 2006 IP
  2. Landeweerd

    Landeweerd Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Thx, but you bether get the variables out of the quotes, example:

    mysql_query("DELETE FROM $table WHERE date < $past") or die ("Error - Unable to delete old visits");

    becomes

    mysql_query("DELETE FROM `".$table."` WHERE `date` < ".$past."") or die ("Error - Unable to delete old visits");
     
    Landeweerd, Dec 25, 2006 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    the code is fine, they are double quotes ....
     
    krakjoe, Dec 25, 2006 IP
    ramakrishna p likes this.
  4. aditya_sfs

    aditya_sfs Peon

    Messages:
    2,271
    Likes Received:
    389
    Best Answers:
    0
    Trophy Points:
    0
    #4

    What is the use of doing that one ??
     
    aditya_sfs, Dec 25, 2006 IP
  5. amitpatel_3001

    amitpatel_3001 Results Follow Patience

    Messages:
    14,074
    Likes Received:
    1,178
    Best Answers:
    0
    Trophy Points:
    430
    #5
    Awesome coding dude :)
    Let me try it on one of my site :)
    Amit
     
    amitpatel_3001, Dec 25, 2006 IP
  6. livingearth

    livingearth Well-Known Member

    Messages:
    1,469
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    140
    #6
    Nice work aditya sfs..
    I have been using an iframe on mine. I think your code to be better and will try it out soon..
    Thanks
     
    livingearth, Dec 25, 2006 IP
  7. aditya_sfs

    aditya_sfs Peon

    Messages:
    2,271
    Likes Received:
    389
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks guys for all comments ;)
     
    aditya_sfs, Dec 25, 2006 IP
  8. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #8
    Nice work.
    I actually did a more complex one that shows what pages users are on and when did they access it.
    www.jstracker.com ( the report here www.jstracker.com/sites.php )
    Technically, it's an off-site script, only 1 line of javascript for you and the rest is done on my machine.

    Green rep though for you.
     
    legend2, Dec 25, 2006 IP
  9. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #9
    As a friendly suggestion...try encapsulating with either functions or as a class. This will make it easier to maintain, extend, and also pass around.
     
    Chemo, Dec 25, 2006 IP
  10. proprod

    proprod Active Member

    Messages:
    216
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    90
    #10
    Thanks for the code,aditya_sfs - May, I add a link to your site as a donation? Please let me know.
     
    proprod, Dec 26, 2006 IP
  11. aditya_sfs

    aditya_sfs Peon

    Messages:
    2,271
    Likes Received:
    389
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thats not needed ...... u can use it freely ;)

    Or if you want, you can feel free to link to my Blog
     
    aditya_sfs, Dec 26, 2006 IP
  12. Freewebspace

    Freewebspace Notable Member

    Messages:
    6,213
    Likes Received:
    370
    Best Answers:
    0
    Trophy Points:
    275
    #12
    I want to ask another question here whether there is any other script to display how may users from each country
     
    Freewebspace, Dec 26, 2006 IP
  13. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #13
    To limit the queries, you could run the update query at first, and use the insert query only when mysql_affected_rows() returns 0.

    Something like that:

    
    
    mysql_query("UPDATE $table SET date='$now' WHERE ip='$_SERVER[REMOTE_ADDR]'") or die ("Error - Unable to update visit");
    
    if (mysql_affected_rows() == 0)
    {
        mysql_query("INSERT INTO $table VALUES ('$_SERVER[REMOTE_ADDR]','$now')") or die ("Error - Unable to insert records");
    }
    
    
    PHP:
     
    nico_swd, Dec 26, 2006 IP
  14. aditya_sfs

    aditya_sfs Peon

    Messages:
    2,271
    Likes Received:
    389
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Hmm yes this seems to be good. Will try it out ;)
     
    aditya_sfs, Dec 26, 2006 IP
  15. toby

    toby Notable Member

    Messages:
    6,923
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    285
    #15
    so jstracker is yours... hmmm, i saw it at the new site i bought> 4july.info

    aditya. thanks for the effort :)
     
    toby, Dec 26, 2006 IP
  16. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #16
    Oh yes jstracker is mine. many sites and/or turnkeys use it :)
     
    legend2, Dec 26, 2006 IP
  17. aditya_sfs

    aditya_sfs Peon

    Messages:
    2,271
    Likes Received:
    389
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Great man. I was just browsing around your site. Looks simple for the users. But i can guess the complexity of the code behind ;)
     
    aditya_sfs, Dec 26, 2006 IP
  18. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #18
    i tried to make it simple for the users. no registration they just get the code to plug.
    The backend is of course a bit more complex:) but it's not that complex as you might think.
    I started small thens tarted adding stuff as time passed by such as the live report.
     
    legend2, Dec 26, 2006 IP
  19. proprod

    proprod Active Member

    Messages:
    216
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    90
    #19
    Hiya, been using for a night or two... tonight yahoo and google bots visited my site. Needless to say, currently, I show 76 visitors (3 actual people). Is there a way to plug some code in to ignore certain IP ranges? To ignore things like googlebot and such.

    Thanks :)
     
    proprod, Dec 26, 2006 IP
  20. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #20
    propod: talking to the thread starter or me? mine counts them accurately.
     
    legend2, Dec 27, 2006 IP