I have written a stat counter for my site of the form
<?php
$db = mysql_connect("localhost", "root") or die ("unable to connect to server.<br>");
mysql_select_db("lithics_site",$db) or die ("Unable to connect to database.<br>");
$result = mysql_query("SELECT count(*) FROM counter",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<P class=normal>Site stats: %s Individual visits.</P>
",
$myrow["count(*)"]);
}
?>
does anyone know how I can have the server skip adding to the database if the ip address has already being listed. so the counter will show the number of individual computers that have accessed the site.
I have a feeling this can be done with if else statements but dont know how to impliment it.
individual counter help
Moderator: General Moderators
-
samscripts
- Forum Commoner
- Posts: 57
- Joined: Tue Apr 23, 2002 4:34 pm
- Location: London, UK
a couple of things you could do to get the number of indiviual ip addresses:
easiest would be to just change the query to:
this wouldn't stop the adding of rows with the same ip though, but shouldn't require any change to your table, and means you can still get other stats like total page hits.
You could make the ip field unique, and then any attempt to insert a hit with the same ip would fail.
Or you could use a select query to see if a record with the ip address already exists and only add the new one if it doesnt.
Hope this helps, Sam
easiest would be to just change the query to:
Code: Select all
SELECT count(DISTINCT ip) FROM counterYou could make the ip field unique, and then any attempt to insert a hit with the same ip would fail.
Or you could use a select query to see if a record with the ip address already exists and only add the new one if it doesnt.
Hope this helps, Sam
- knmwt15000
- Forum Newbie
- Posts: 8
- Joined: Thu Apr 25, 2002 9:51 am
I know this is an insanly old post origanlly... but I thought I'd add my two since worth...
You could run a DELETE FROM querry as well just make sure to tage LIMIT 1 to the end of it.. or you'll delete both ip's.
Also make sure to run it after the entry, and before the stats output... it may be just a couple of extra steps, but its something I did with an update page for incoming payments to keep me from accidently inserting nasty blank fields all over the place.
You could run a DELETE FROM querry as well just make sure to tage LIMIT 1 to the end of it.. or you'll delete both ip's.
Also make sure to run it after the entry, and before the stats output... it may be just a couple of extra steps, but its something I did with an update page for incoming payments to keep me from accidently inserting nasty blank fields all over the place.