June 12, 200818 yr hi again i have a php script for a basic hit counter,the only problem is when ever i change paget it increases by 1 giving a false reading of the ammount of visiters how can i change it so that it only registers the 1 hit no matter how many pages are viewed thanks again <?php $host="localhost"; $username="******"; $password="******"; $db_name="test"; $tbl_name="counter"; mysql_connect("$host", "$username", "$password")or die("cannot connect to server "); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); $counter=$rows['counter']; if(empty($counter)){ $counter=1; $sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')"; $result1=mysql_query($sql1); } echo "You are visitor No. "; echo $counter; $addcounter=$counter+1; $sql2="update $tbl_name set counter='$addcounter'"; $result2=mysql_query($sql2); mysql_close(); ?> can i add a session or a if else function to make this work thank you
June 12, 200818 yr Two ways of doing it really. You can either set a cookie that tells the counter that they have already been logged down, or a more basic solution would be to check where the user has come from using HTTP_REFERRER. So if the previous page was outside of your domain, it'll log a hit, if the previous page is within your domain, it won't. It'll still log multiple hits if someone leaves your site then comes back, but thats better than it logging on every page. You'll want something along the lines of... if (($_SERVER['HTTP_REFERER']) !== ($_SERVER[HTTP_HOST])) { // Log down a hit } else { // Do nothing } That won't work how you want it, but with some tweaking you should be on your way.
Create an account or sign in to comment