Page 1 of 1

user online duplicate checking

Posted: Sun Mar 14, 2004 12:22 pm
by noaksey2000
okok this doesnt differ much from a post earlier on but ok i have the script:

Code: Select all

$server = "";
$db_user = "";
$db_pass = "";
$db = "";
$to_secs = 180;

$t_stamp = time();
$timeout = $t_stamp - $to_secs;

mysql_connect($server, $db_user, $db_pass) or die ("Useronline Database CONNECT Error");

mysql_db_query($db, "INSERT INTO userson VALUES ('$t_stamp','$REMOTE_ADDR','$PHP_SELF')") or die("Database INSERT Error");
mysql_db_query($db, "DELETE FROM userson WHERE timestamp<$timeout") or die("Database DELETE Error");
$result = mysql_db_query($db, "SELECT ip FROM userson WHERE file='$PHP_SELF'") or die("Database SELECT Error");
$user = mysql_num_rows($result);
echo $user;
Right so everytime the user reload the page it will store a unix timestamp, an ip address and the page name in a new row. Is there anyway to stop duplicates from being entered into the database, i.e. if the users ip address already exists in one of the rows, skip the insert part so it then can count the number of rows 'correctly'?

cheers for any help,
chris.

Posted: Mon Mar 15, 2004 1:58 pm
by JAM
Rather, use:

Code: Select all

mysql_db_query($db, "SELECT count(distinct ip) FROM userson WHERE file='$PHP_SELF'");
Note the distinct part. That way you only select, well, distinct, ips... There are other ways to do it, but this is by far the easiest way.

Fetch the above result by using [php_man]mysql_result[/php_man]() or similiar...