user online duplicate checking

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
noaksey2000
Forum Newbie
Posts: 5
Joined: Sun Aug 17, 2003 10:29 am

user online duplicate checking

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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...
Post Reply