Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

online users

Status
Not open for further replies.

rs51

Technical User
Oct 13, 2001
163
0
0
PT
hi
i'm still in the beguining of php and copy/paste this script, which shows the number of users online. I presume i gave 10 minutes to active user being in the site, and after it it was suposed the script deleted it from database.
But that isnt happening, and why i dont know.

i've a table:

CREATE TABLE users_online (
horas int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
ficheiro varchar(100) NOT NULL,
PRIMARY KEY (horas),
KEY ip (ip),
KEY ficheiro (ficheiro)
);

here's the code:

<?php

$server = &quot;***&quot;;

$db_user = &quot;***&quot;;

$db_pass = &quot;***&quot;;

$database = &quot;***&quot;;

$tempofora = 600;

$PHP_SELF = $_SERVER['PHP_SELF'];

$horas = time();

$timeout = $horas-$tempofora;

$REMOTE_ADDR=getenv(&quot;REMOTE_ADDR&quot;);

mysql_connect($server, $db_user, $db_pass);

$adiciona = mysql_db_query($database, &quot;INSERT INTO users_online VALUES

('$horas','$REMOTE_ADDR','$PHP_SELF')&quot;);

if(!($adiciona)) {

print &quot;Ocorreu um erro!&quot;;

}

$apaga = mysql_db_query($database, &quot;DELETE FROM users_online WHERE $horas < $timeout&quot;);

if(!($apaga)) {

print &quot;Ocorreu um erro!&quot;;

}

$resultado = mysql_db_query($database, &quot;SELECT DISTINCT ip FROM
users_online WHERE ficheiro='$PHP_SELF'&quot;);

if(!($resultado)) {

print &quot;Ocorreu um erro!&quot;;

}

$user = mysql_num_rows($resultado);

mysql_close();

if($user == 1) {

print(&quot;$user utilizador online\n&quot;);

} else {

print(&quot;$user utilizadores online\n&quot;);

}

?>


The line of code i dont understand how it works is this one:
$apaga = mysql_db_query($database, &quot;DELETE FROM users_online WHERE $horas < $timeout&quot;);
doesnt the script should delete olders entries?
Can someone help me please, since i can see from database that the script doesnt clear old entries...
 
Dear rs51,
I'm not sure, but I think the problem could be that you need to subtract your timeout time from the current time before performing the query:

$cur_time = time();

$pippo = $cur_time-($timeout*60); //where $timeout is in minutes

$apaga = mysql_db_query($database, &quot;DELETE FROM users_online WHERE $horas < $pippo&quot;);

hope it helps
Gaetano
 
hi thank you for helping
yes, you'r right
i've a mistake in the insert query
should be:
$apaga = mysql_db_query($database, &quot;DELETE FROM users_online WHERE NAME_OF_THE_FIELD< $pippo&quot;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top