jmr3460 Posted June 10, 2009 Share Posted June 10, 2009 I just added a couple of lines that I hope will stop the bots and spiders from being counted. I just don't know if when I exit will it kill the script successfully, and not count them. Here is the code: <?php include("db_in.php"); $table = "table_name"; $ip = getenv('REMOTE_ADDR'); $date = date("M-d-Y h:i:s", time()+3600); $browser = $_SERVER['HTTP_USER_AGENT']; //a defined array with a list of bots in it, defined as $bot include("botlist.php"); if ($browser == $bot[]) { exit } else{ if (!isset($_COOKIE['clean'])) { setcookie("clean","cleantime",time()+7200,"/"); mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); mysql_query("INSERT INTO $table values('id','$ip','$date','$browser')") or die(mysql_error()); $data = mysql_query("SELECT id FROM $table ORDER BY id DESC LIMIT 1;"); $info = mysql_fetch_array($data); $count = $info['id']; } else { mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); $data = mysql_query("SELECT id FROM $table ORDER BY id DESC LIMIT 1;"); $info = mysql_fetch_array($data); $count = $info['id']; } } ?> <html> <?php echo $count; ?> </html> Here is my botlist.php <?php $bot = array("YandexSomething/1.0", "Baiduspider+(+http://www.baidu.com/search/spider.htm)", "TurnitinBot/2.1 (http://www.turnitin.com/robot/crawlerinfo.html)", "LinkWalker/2.0", "Gigabot/3.0 (http://www.gigablast.com/spider.html)", "Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)"); ?> Thanks for any help Link to comment https://forums.phpfreaks.com/topic/161618-anti-bot-list-added-to-my-counter/ Share on other sites More sharing options...
Philip Posted June 10, 2009 Share Posted June 10, 2009 You need to use in_array: if(in_array($browser, $bot)) { exit(); } else { //... instead of if ($browser == $bot[]) { exit } else{ //... Link to comment https://forums.phpfreaks.com/topic/161618-anti-bot-list-added-to-my-counter/#findComment-852832 Share on other sites More sharing options...
jmr3460 Posted June 10, 2009 Author Share Posted June 10, 2009 Thanks I will try that and get back to report. Link to comment https://forums.phpfreaks.com/topic/161618-anti-bot-list-added-to-my-counter/#findComment-852834 Share on other sites More sharing options...
ted_chou12 Posted June 10, 2009 Share Posted June 10, 2009 A suggestion, the code that you use now doesn't work for general bots, ie. if a yahoo or a msn bot comes, it would not catch that, a better way is to match up more general ones: $useragent = $_SERVER['HTTP_USER_AGENT']; $result = false; $searchengines = array("bot", "crawler", "spider", "google", "yahoo", "msn", "ask", "ia_archiver"); foreach ($searchengines as $searchengine) { $match = "/$searchengine/i"; if (preg_match($match, $useragent)) {$result = true;}} if ($result == true) {//counter don't count bot} Ted [/code] Link to comment https://forums.phpfreaks.com/topic/161618-anti-bot-list-added-to-my-counter/#findComment-852845 Share on other sites More sharing options...
jmr3460 Posted June 11, 2009 Author Share Posted June 11, 2009 Is this a $searchengines = array("bot", "crawler", "spider", "google", "yahoo", "msn", "ask", "ia_archiver"); I just got the above code inserted so I did not get any warnings. Everything seems to count. I got this hit two minutes after I got it going. Mozilla/5.0 (compatible; Yahoo! Slurp/3.0; http://help.yahoo.com/help/us/ys Here is how I inserted the code above. I did change one thing. I changed $useragent to $browser because this is what I used for my counter. <?php include("dbin.php"); $ip = getenv('REMOTE_ADDR'); $date = date("M-d-Y h:i:s", time()+3600); $table = "home"; $browser = $_SERVER['HTTP_USER_AGENT']; $result = "false"; $searchengines = array("bot", "crawler", "spider", "google", "yahoo", "msn", "ask", "ia_archiver"); foreach ($searchengines as $searchengine) { $match = "/$searchengine/i"; if (preg_match($match, $browser)) {$result = true;}} if ($result == true) {//Counter script here if (!isset($_COOKIE['clean'])) { setcookie("clean","cleantime",time()+7200,"/"); mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); mysql_query("INSERT INTO $table values('id','$ip','$date','$browser')") or die(mysql_error()); $data = mysql_query("SELECT id FROM home ORDER BY id DESC LIMIT 1;"); $info = mysql_fetch_array($data); $count = $info['id']; } else { mysql_connect($host, $user, $pass); mysql_select_db($database) or die(mysql_error()); $data = mysql_query("SELECT id FROM $table ORDER BY id DESC LIMIT 1;"); $info = mysql_fetch_array($data); $count = $info['id']; } } ?> <html> <?php echo $count; ?> </html> Everything seems to count but I think this is something I am trying not to count. Why will my anti bot code not work. I was thinking that as they get counted I can review my report page and add any bots to my list. I am hoping that eventually catch a very large majority of them. If this one works! You can see my report file at http://www.arscna.org/print.php Sorry this is such a long post. Link to comment https://forums.phpfreaks.com/topic/161618-anti-bot-list-added-to-my-counter/#findComment-853566 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.