Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    37
    Thanks
    1
    Thanked 0 Times in 0 Posts

    $timeout = time()-1*60;

    I have a guest’s online script that adds ip address, time and current page there on into mysql table.

    This all works perfectly the guest gets added into the table it’s when it comes to deleting the guest when there has been no activity from them for a set period of time. No matter what i try the guest does not get removed from the database?

    dbase.php

    $timeout = time()-1*60;
    $q = "DELETE FROM guests WHERE timestamp < $timeout";
    mysql_query($q, $this->connection);


    Table details:
    Field: timestamp Type: int(15) default: 0

    Time stamp example 1200057587

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    This is perhaps the query you want to be using:
    PHP Code:
    $delete_old "DELETE FROM useronline WHERE (( $time - timestamp) > 900)";
    mysql_query($delete_old); 
    change the 900 to what ever time you want (300 - 5mins)
    Last edited by srule_; 01-11-2008 at 02:15 PM.

  • #3
    New Coder
    Join Date
    Oct 2007
    Posts
    37
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by srule_ View Post
    This is perhaps the query you want to be using:
    PHP Code:
    $delete_old "DELETE FROM useronline WHERE (( $time - timestamp) > 900)";
    mysql_query($delete_old); 
    change the 900 to what ever time you want (300 - 5mins)
    So $time is pointing to?

    $time = time();

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    yes :-)

    here is the complete scipt:

    PHP Code:
    <?php
    $username 
    $_SESSION['username'];
    $time time(); 

    $ip_check "SELECT username FROM useronline WHERE username ='$username'";
    $ip_query mysql_query($ip_check);
    $num_rows mysql_num_rows($ip_query); 

    if(!
    $num_rows) { //if no rows, meaning no ip's in db matched theirs, we will add them.
       
    $insert_new mysql_query("INSERT INTO useronline (timestamp, ip, username) VALUES ('$time','test', '{$_SESSION[username]}')");
        
    $updateLast mysql_query("UPDATE resources SET last_seen='$time' WHERE username = '$username'");
        if(!
    $updateLast) die(mysql_error());  
    //end if NOT THERE

    //this means that they're already in there, so we update info.
    if($num_rows 0) {
          
    $update mysql_query("UPDATE useronline SET timestamp='$time' WHERE username = '$username'");
           if(!
    $update) die(mysql_error());
           
    $updateLast mysql_query("UPDATE resources SET last_seen='$time' WHERE username = '$username'");
           if(!
    $updateLast) die(mysql_error());
    // end UPDATE 

    //if time now - start time > 300 then its been 5 minutes, so we delete
    $delete_old mysql_query("DELETE FROM useronline WHERE (( $time - timestamp) > 900)");
    if(!
    $delete_old) die(mysql_error()); 

    //show the number of people online now....
    $id_query = @mysql_query("SELECT * FROM useronline");
    $users_online mysql_num_rows($id_query);

    //users online is done.... 
    ?>

    Change the details for your database then include that in any page (after a database connection) and echo $users_online to get the number of ppl online.

    Database only needs two fields: 1-User identity (ip, username, ect..) 2-Time (holds the timestamp)

  • #5
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    here is some codes mate that i did personally for you

    Code:
    Done by andre3 :P
    for additional support mail me at:
    ----Postmaster@mobilespot.ws------
    
    
    ======THis bits deal with the time thing======
    $timenow = $_GET["timenow"];
    $timeout = $_GET["timeout"];
    
    $timenow = $time();
    $timeout = $time+60;
    
    
    =======This bits do the deleting==========
    mysql_query("DELETE FROM table_name WHERE expires < '".$timenow."'");
    
    
    		
    ======Now we do a count. if it returns 0 we insert..if it returns a greater value we update============
    
    $count = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM table_name WHERE ip='".$ip_variable."'"));
    
    
    =========insert bits==========================
    if($count[0]=="0"){
    
    $sql = mysql_query("INSERT INTO table_name SET ip='".$ip-variable."', time='".$time."', expires='".$timeout."', page='".$page-variable."'");
               }
    ==========Update Bits==========================================
             else{
    
    $update = mysql_query("UPDATE table_name SET expires='".$timeout."', time='".$time."', page='".$page-variable."' WHERE ip='".$ip_variable."'");
                      }

  • #6
    New Coder
    Join Date
    Oct 2007
    Posts
    37
    Thanks
    1
    Thanked 0 Times in 0 Posts
    are you able to tell me why my script does not work?

  • #7
    New Coder
    Join Date
    Oct 2007
    Posts
    37
    Thanks
    1
    Thanked 0 Times in 0 Posts
    After making the changes and testing i found for some strange reason. It updates the guest into the table but does not delete them after a set time. Instead it replaces them with the latest guest to visit the site? So i only get a count of 1 guest and thats the last guest to visit the site.

    Any ideas as to why this is happening???


  •  

    Posting Permissions

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