Dave Fes
Dave Fes

Reputation: 243

How to stop bots from running some code on PHP script?

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!

Upvotes: 1

Views: 4515

Answers (3)

aynber
aynber

Reputation: 23000

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.

Upvotes: 5

Zevi Sternlicht
Zevi Sternlicht

Reputation: 5399

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

Upvotes: 2

Legion
Legion

Reputation: 836

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

Upvotes: 1

Related Questions