例1,php判断是否搜索引擎蜘蛛:
复制代码 代码示例:
function getrobot() {
if(!defined('IS_ROBOT')) {
$kw_spiders = 'Bot|Crawl|Spider|slurp|sohu-search|lycos|robozilla';
$kw_browsers = 'MSIE|Netscape|Opera|Konqueror|Mozilla';
if(!strexists($_SERVER['HTTP_USER_AGENT'], 'http://') && preg_match("/($kw_browsers)/i", $_SERVER['HTTP_USER_AGENT'])) {
define('IS_ROBOT', FALSE);
} elseif(preg_match("/($kw_spiders)/i", $_SERVER['HTTP_USER_AGENT'])) {
define('IS_ROBOT', TRUE);
} else {
define('IS_ROBOT', FALSE);
}
}
return IS_ROBOT;
}
此函数来源于dz论坛程序. IS_ROBOT 被定义为一个常量.
IS_ROBOT?:'你是搜索引擎蜘蛛吧':'你是人';
例2,php判断是否手机访问:
复制代码 代码示例:
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|mobile|wap)/i";
if(($ua == '' || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER['REQUEST_URI']),'wap'))
{
$Loaction = 'wap/';
if (!emptyempty($Loaction))
{
ecs_header("Location: $Loactionn");
exit;
}
}
有网友提示找不到strexists函数,可以参考如下:
复制代码 代码示例:
function strexists($string, $find) {
return !(strpos($string, $find) === FALSE);
}