I'm using this script that displays the number of users currently online using mysql and php. However, when two users visit the site at the exact same time one user will be able to see it and the other will get the error:
Useronline Database INSERT Error
The database schema is:
Code:
CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);
The php script is:
Code:
$timeoutseconds = 300; // Timeout value in seconds
$timestamp=time();
$timeout=$timestamp-$timeoutseconds;
mysql_connect($server, $db_user, $db_pass) or die ("Useronline Database CONNECT Error");
mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("Useronline Database INSERT Error");
mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error");
$result=mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'") or die("Useronline Database SELECT Error");
$user =mysql_num_rows($result);
mysql_close();
if ($user==1) {echo"<br /><b>$user User online</b>";} else {echo"<br /><b>$user Users online</b>";}
?>
Anyway I might get around this? Do I just need to code something new?
|