如何使用php阻止一些http用户代理 [英] How to block some of http user agent using php
本文介绍了如何使用php阻止一些http用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法通过 php 脚本阻止一些用户代理?mod_security 示例
there have a way to block some of user agent via php script? Example on mod_security
SecFilterSelective HTTP_USER_AGENT "Agent Name 1"
SecFilterSelective HTTP_USER_AGENT "Agent Name 2"
SecFilterSelective HTTP_USER_AGENT "Agent Name 3"
我们也可以通过示例使用 htaccess 或 robots.txt 阻止它们,但我想要在 php 中.有示例代码吗?
Also we can block them using htaccess or robots.txt by example but I want in php. Any example code?
推荐答案
我喜欢 @Nerdling 的回答,但如果它有帮助,如果您有很长的用户代理列表需要阻止:
I like @Nerdling's answer, but in case it's helpful, if you have a very long list of user agents that need to be blocked:
$badAgents = array('fooAgent','blahAgent', 'etcAgent');
foreach($badAgents as $agent) {
if(strpos($_SERVER['HTTP_USER_AGENT'],$agent) !== false) {
die('Go away');
}
}
更好:
$badAgents = array('fooAgent','blahAgent', 'etcAgent');
if(in_array($_SERVER['HTTP_USER_AGENT'],$badAgents)) {
exit();
}
这篇关于如何使用php阻止一些http用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文