1

I have a script which will add to a databse everytime a page is viewed, I don't want bots triggering the script portion which adds to the database. Basically, I want to only log real users.

Anyway to have bots ignore a section of PHP script?

Thanks!

Termininja
  • 6,620
  • 12
  • 48
  • 49
Dave Fes
  • 233
  • 2
  • 5
  • 17
  • 2
    this has some information http://stackoverflow.com/questions/3560613/php-differentiate-between-a-human-user-and-a-bot-other – Jeff Hawthorne Jul 11 '13 at 17:32

3 Answers3

5

While not foolproof, you can check the USER_AGENT string and only run that code if 'bot' does not exist.

if(stripos($_SERVER['HTTP_USER_AGENT'],'bot') === false){ }

This would stop any bot that actually has bot in the user agent string that also does not pay attention to robots.txt.

aynber
  • 22,380
  • 8
  • 50
  • 63
  • this seems the best. will do it now. Thank you sir. – Dave Fes Jul 11 '13 at 17:47
  • Sometimes HTTP_USER_AGENT can be empty and also "crawl" can be needed, so I think a more complete answer could be: `if (!empty($_SERVER['HTTP_USER_AGENT']) and preg_match('~(bot|crawl)~i', $_SERVER['HTTP_USER_AGENT'])) { ... }` Answer found at https://stackoverflow.com/a/17515475/907736 – Lucas Oct 23 '21 at 15:27
1

you could make a robots.txt file restricting bots from accessing the pages or directories that you don't want them too. Here is a link that will show you how the robots.txt works Robots.txt

Legion
  • 796
  • 7
  • 12
1

Why not do something like this, it will only request that the bots stay away, whether they do or dont is up to their discretion!

User-agent: *
Disallow: /my_page.php
Zevi Sternlicht
  • 5,399
  • 19
  • 31
  • I want the bot to access the page, but not the last section of code. Should I make the last section of code in a separate PHP file and then run that file using an 'include'? but make the bots ignore the included file? i'm not sure that will work – Dave Fes Jul 11 '13 at 17:37
  • It's better practice to Disallow all pages, and explicitly Allow the ones you want to be accessed. Otherwise, miscreants can look through your robots.txt file and see all the files you'd like protecting, and then load them up to see why..... – andrewsi Jul 11 '13 at 17:37
  • @andrewsi not always a practical solution, and you can always comment to say that it is not to trigger the counter! – Zevi Sternlicht Jul 11 '13 at 17:38
  • @DaveFes no that wont help, it either sees everything included or nothing. You will have to check the user agent string to see if it is a bot and not echo it out at all for that! – Zevi Sternlicht Jul 11 '13 at 17:39