Page 1 of 1

individual counter help

Posted: Thu Apr 25, 2002 9:51 am
by knmwt15000
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.

Posted: Thu Apr 25, 2002 10:27 am
by samscripts
a couple of things you could do to get the number of indiviual ip addresses:
easiest would be to just change the query to:

Code: Select all

SELECT count(DISTINCT ip) FROM counter
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

Posted: Fri Apr 26, 2002 3:58 am
by knmwt15000
thanks mate.

I actually made the field unique yesterday but I'm going to change it back and use your first suggestion.

cheers

Posted: Sun Nov 24, 2002 3:15 am
by aybra
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.