How to stop bots from running some code on PHP scr

2019-08-14 01:56发布

问题:

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!

回答1:

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.



回答2:

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



回答3:

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