...

View Full Version : $timeout = time()-1*60;



wolfandcrow
01-11-2008, 02:39 PM
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

srule_
01-11-2008, 03:09 PM
This is perhaps the query you want to be using:


$delete_old = "DELETE FROM useronline WHERE (( $time - timestamp) > 900)";
mysql_query($delete_old);


change the 900 to what ever time you want (300 - 5mins)

wolfandcrow
01-11-2008, 03:28 PM
This is perhaps the query you want to be using:


$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();

srule_
01-11-2008, 04:09 PM
yes :-)

here is the complete scipt:



<?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)

andre3
01-11-2008, 04:55 PM
here is some codes mate that i did personally for you



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."'");
}

wolfandcrow
01-14-2008, 09:22 AM
are you able to tell me why my script does not work?

wolfandcrow
01-14-2008, 07:22 PM
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???



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum