Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,431 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,905 people online right now. Registration is fast and FREE... Join Now!




blocking robots from hit counter

 
Reply to this topicStart new topic

blocking robots from hit counter

wwonder
9 Jan, 2008 - 09:39 AM
Post #1

New D.I.C Head
*

Joined: 16 Apr, 2007
Posts: 20


My Contributions
hi

i have a hit counter that counts unique ip addresses i have worked out how to block my ip address so that whilst designing i do not add to the counter but how would i block google robots etc from increasing the counter - code is as follows:

CODE

<?
// includes connection file
include("dbconnect.inc.php");

$ip = $_SERVER['REMOTE_ADDR'];
$date = date('d.m.Y');
$block = "195.93.21..*";

$MySql = "SELECT * FROM counter WHERE ip = '$ip' AND date = '$date';";
$get = mysql_query($MySql) or die(mysql_error());

if (ereg($block, $ip)) {

}else{
// Counts the rows found
if(mysql_num_rows($get)==0) {
// If no rows where found
$select = mysql_query("INSERT INTO counter (ip, date, hits) VALUES ('$ip', '$date', '1')");
// Inserts into the database, there ip and date of visit
} else {
// If they have been
$hit = mysql_fetch_array($get);
// selects the data from the database

$hits = $hit['hits'] + 1;

// Adds one onto current hit counter
$select = mysql_query("UPDATE counter SET hits = '$hits' WHERE ip = '$ip' AND date = '$date'");
// Updates Database with there new hit count
}}

?>

*edit: Please use code tags in the future. Thanks! code.gif

thanks

This post has been edited by Martyr2: 9 Jan, 2008 - 12:52 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Blocking Robots From Hit Counter
9 Jan, 2008 - 12:58 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,260



Thanked: 227 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well one thing you can do is check the visitors user agent string and compare it against a list like found at Useragentstring.com and look for specific keywords to then not count. For example if the user agent string has "googlebot" in it then don't count the hit.

Check out the $_SERVER[] predefined constant array for "HTTP_USER_AGENT" and use that in your script to check the user agent string.

Just so you know, you are not going to be able to block all bots all the time. But you can significantly reduce the hits bots make by checking them against known bots and their user agent strings that are recorded.

Hope this helps!

"At DIC we be user agent checking (and female body checking) code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:58AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month