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

*爱你&永不变心* 提交于 2019-12-11 04:58:51

问题


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


来源:https://stackoverflow.com/questions/17599820/how-to-stop-bots-from-running-some-code-on-php-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!