SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Users Online PHP

    usersonline.php

    Code:
    <?                                                                                                            
    $server         	= "localhost";  		                
    $db_user        	= "username"; 		                                     
    $db_pass        	= "pass";			                                      
    $database       	= "db";	                                          
    
    $timeoutseconds 	= 3600;			        
    
    $timestamp=time();                                                                                            
    $timeout=$timestamp-$timeoutseconds;  
    mysql_connect($server, $db_user, $db_pass) or die ("Proxies R Us has experienced an update or database error - please refresh page to fix!");                                                                   
    mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("Useronline Database INSERT Error"); 
    mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error");
    $result=mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'") or die("Useronline Database SELECT Error");
    $user  =mysql_num_rows($result);                                                                              
    mysql_close();                                                                                                
    if ($user==1) {echo"<font size=1>There is curently $user user browsing this page</font>";} else {echo"<font size=1>There are currently $user users browsing this page</font>";}
    ?>
    db:

    Code:
    #
    # Table structure for table 'useronline'
    #
    
    CREATE TABLE useronline (
       timestamp int(15) DEFAULT '0' NOT NULL,
       ip varchar(40) NOT NULL,
       file varchar(100) NOT NULL,
       PRIMARY KEY (timestamp),
       KEY ip (ip),
       KEY file (file)
    );
    Okay, so this script works. When include it on a page, it displays the number of guests browsing the page. My question is, would there be a way to make that number universal, so instead of counting the number of guests on a particular page, the number of guests through the entire website/domain. Is this doable? I have seen it done before.

    Thanks

    CWN

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're now using a SELECT query to get visitors browsing the current page. Change it to:
    Code:
    SELECT DISTINCT ip FROM useronline
    Note, I removed the condition part.
    Saul

  3. #3
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, what did you mean by "I've removed the condition part?"

    As well, don't I already have SELECT DISTINCT ip FROM useronline?

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Exactly, you do have this query. So in order to display online users from all over the site, I removed the one page restriction. Try this code:
    PHP Code:
    <?                                                                                                            
    $server             
    "localhost";                          
    $db_user            "username";                                              
    $db_pass            "pass";                                                  
    $database           "db";                                              

    $timeoutseconds     3600;                    

    $timestamp=time();                                                                                            
    $timeout=$timestamp-$timeoutseconds;  
    mysql_connect($server$db_user$db_pass) or die ("Proxies R Us has experienced an update or database error - please refresh page to fix!");                                                                   
    mysql_db_query($database"INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("Useronline Database INSERT Error"); 
    mysql_db_query($database"DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error");
    $result=mysql_db_query($database"SELECT DISTINCT ip FROM useronline") or die("Useronline Database SELECT Error");
    $user  =mysql_num_rows($result);                                                                              
    mysql_close();                                                                                                
    if (
    $user==1) {echo"<font size=1>There is curently $user user browsing this page</font>";} else {echo"<font size=1>There are currently $user users browsing this page</font>";}
    ?>
    Saul

  5. #5
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked, thanks!

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're welcome.
    Saul

  7. #7
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The WHERE part limits your search to just that page.

    "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'"

    The conditional is the part in bold which is what is limiting your search for usersonline to only that page.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  8. #8
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah! Thanks for the info

  9. #9
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, guys. So using the script and info above...

    I would like to display the following information:

    timestamp field and file field (only currently online ones)

    Does someone know how to do that? This would be on a seperate page, so it's okay if the "users online" thing is not there.

  10. #10
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I guess that's what you wan't to do:
    PHP Code:
    <?                                                                                                            
    $server             
    "localhost";                          
    $db_user            "username";                                              
    $db_pass            "pass";                                                  
    $database           "db";                                              

    $timeoutseconds     3600;                    

    $timestamp=time();                                                                                            
    $timeout=$timestamp-$timeoutseconds;  
    mysql_connect($server$db_user$db_pass) or die ("Proxies R Us has experienced an update or database error - please refresh page to fix!");                                                                   
    mysql_db_query($database"INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("Useronline Database INSERT Error"); 
    mysql_db_query($database"DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error");
    $result=mysql_db_query($database"SELECT file, timestamp FROM useronline GROUP BY file") or die("Useronline Database SELECT Error");
    while ( 
    $row mysql_fetch_assoc($result) ){
      echo 
    $row['file']." - ".$row['timestamp']."<br />";
    }                                                                            
    mysql_close();
    ?>
    Saul

  11. #11
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How can we make it display ONLY whats in the $timeoutseconds ?

    that script didn't work btw -

    "Useronline Database INSERT Error"

  12. #12
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Strange, I didn't change that part...

    Can you explain more what you're trying to do here?
    Saul

  13. #13
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would like to display "file" on a php page, but only entires of file that are within the
    $timeoutseconds = 3600;

    Do you know what I mean?

  14. #14
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh, I see. Well, since your script removes older records, this should work:
    PHP Code:
     <?                                                                                                            
    $server             
    "localhost";                          
    $db_user            "username";                                              
    $db_pass            "pass";                                                  
    $database           "db";                                              

    $timeoutseconds     3600;                    

    $timestamp=time();                                                                                            
    $timeout=$timestamp-$timeoutseconds;  
    mysql_connect($server$db_user$db_pass) or die ("Proxies R Us has experienced an update or database error - please refresh page to fix!");                                                                   
    mysql_db_query($database"INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("Useronline Database INSERT Error"); 
    mysql_db_query($database"DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error");
    $result=mysql_db_query($database"SELECT DISTINCT file FROM useronline") or die("Useronline Database SELECT Error");
    while ( 
    $row mysql_fetch_assoc($result) ){
      echo 
    $row['file']."<br />";
    }                                                                            
    mysql_close();
    ?>
    Saul

  15. #15
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I think there is a slight error in coding:

    http://proxiesrus.com/usersonline.php

    Thanks though Any idea why it does that?

  16. #16
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't get it what could be wrong. Can you try this code, it'll give more details about the error:
    PHP Code:
     <?                                                                                                            
    $server             
    "localhost";                          
    $db_user            "username";                                              
    $db_pass            "pass";                                                  
    $database           "db";                                              

    $timeoutseconds     3600;                    

    $timestamp=time();                                                                                            
    $timeout=$timestamp-$timeoutseconds;  
    mysql_connect($server$db_user$db_pass) or die ("Proxies R Us has experienced an update or database error - please refresh page to fix!");                                                                   
    mysql_db_query($database"INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("Useronline Database INSERT Error: ".mysql_error()); 
    mysql_db_query($database"DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error");
    $result=mysql_db_query($database"SELECT DISTINCT file FROM useronline") or die("Useronline Database SELECT Error");
    while ( 
    $row mysql_fetch_assoc($result) ){
      echo 
    $row['file']."<br />";
    }                                                                            
    mysql_close();
    ?>
    Saul

  17. #17
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://proxiesrus.com/usersonline.php

    Werid message now! Does that help?

  18. #18
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, your table structure is wrong, you've set timestamp as primary key, but it can be duplicate, thus the error. Try changing it to:
    Code:
    CREATE TABLE useronline (
       id int(11) NOT NULL auto_increment,
       timestamp int(15) DEFAULT '0' NOT NULL,
       ip varchar(40) NOT NULL,
       file varchar(100) NOT NULL,
       PRIMARY KEY (id),
       KEY (timestamp),
       KEY ip (ip),
       KEY file (file)
    );
    Saul

  19. #19
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, when I do that - my other feature "displaying users online" doesnt' work

  20. #20
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What's happening?
    Saul

  21. #21
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?
    $server = "localhost";
    $db_user = "username";
    $db_pass = "pass";
    $database = "db";

    $timeoutseconds = 3600;

    $timestamp=time();
    $timeout=$timestamp-$timeoutseconds;
    mysql_connect($server, $db_user, $db_pass) or die ("Proxies R Us has experienced an update or database error - please refresh page to fix!");
    mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("Useronline Database INSERT Error");
    mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error");
    $result=mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'") or die("Useronline Database SELECT Error");
    $user =mysql_num_rows($result);
    mysql_close();
    if ($user==1) {echo"<font size=1>There is curently $user user browsing this page</font>";} else {echo"<font size=1>There are currently $user users browsing this page</font>";}
    ?>

    That code doesn't work with your new table structure

  22. #22
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ooops, sorry, forgot one thing. Here's for the online users:
    PHP Code:
    <?
    $server 
    "localhost";
    $db_user "username";
    $db_pass "pass";
    $database "db";

    $timeoutseconds 3600;

    $timestamp=time();
    $timeout=$timestamp-$timeoutseconds;
    mysql_connect($server$db_user$db_pass) or die ("Proxies R Us has experienced an update or database error - please refresh page to fix!");
    mysql_db_query($database"INSERT INTO useronline (timestamp,ip,file) VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("Useronline Database INSERT Error");
    mysql_db_query($database"DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error");
    $result=mysql_db_query($database"SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'") or die("Useronline Database SELECT Error");
    $user =mysql_num_rows($result);
    mysql_close();
    if (
    $user==1) {echo"<font size=1>There is curently $user user browsing this page</font>";} else {echo"<font size=1>There are currently $user users browsing this page</font>";}
    ?>
    And for files:
    PHP Code:
    <?                                                                                                            
    $server             
    "localhost";                          
    $db_user            "username";                                             
    $db_pass            "pass";                                                  
    $database           "db";                                              

    $timeoutseconds     3600;                    

    $timestamp=time();                                                                                            
    $timeout=$timestamp-$timeoutseconds;  
    mysql_connect($server$db_user$db_pass) or die ("Proxies R Us has experienced an update or database error - please refresh page to fix!");                                                                   
    mysql_db_query($database"INSERT INTO useronline (timestamp,ip,file) VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("Useronline Database INSERT Error: ".mysql_error());
    mysql_db_query($database"DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error");
    $result=mysql_db_query($database"SELECT DISTINCT file FROM useronline") or die("Useronline Database SELECT Error");
    while ( 
    $row mysql_fetch_assoc($result) ){
      echo 
    $row['file']."<br />";
    }                                                                            
    mysql_close();
    ?>
    Saul

  23. #23
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked! You're awesome! You've been so, so, so helpful!

    http://proxiesrus.com/usersonline.php

    It looks great - I now need to establish the timestamp, in the other column.

  24. #24
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You are welcome.
    Saul


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •