首页
> 计算机技术
> 后端开发
> PHP
PHP语言中如何判断请求是来自电脑还是手机的方法
原创 lihf8515于2025年02月20日 20:10发表
来源:本站 阅读:93
PHP语言中如何判断请求是来自电脑还是手机的方法,主要是通过 $_SERVER['HTTP_USER_AGENT'] 变量中所包含的值的情况做出判断。
具体实现函数如下:
//判断客户端是否手机 | |
function is_mobile(){ | |
$agent = empty($_SERVER['HTTP_USER_AGENT']) ? '未知' : $_SERVER['HTTP_USER_AGENT']; | |
if(preg_match('/nokia/i', $agent) || preg_match('/sony/i', $agent) || preg_match('/ericsson/i', $agent) || preg_match('/mot/i', $agent) | |
|| preg_match('/samsung/i', $agent) || preg_match('/htc/i', $agent) || preg_match('/sgh/i', $agent) || preg_match('/lg/i', $agent) | |
|| preg_match('/blackberry/i', $agent) || preg_match('/meizu/i', $agent) || preg_match('/sharp/i', $agent) || preg_match('/sie-/i', $agent) | |
|| preg_match('/philips/i', $agent) || preg_match('/panasonic/i', $agent) || preg_match('/alcatel/i', $agent) || preg_match('/lenovo/i', $agent) | |
|| preg_match('/iphone/i', $agent) || preg_match('/ipod/i', $agent)|| preg_match('/netfront/i', $agent) || preg_match('/symbian/i', $agent) | |
|| preg_match('/ucweb/i', $agent)|| preg_match('/windowsce/i', $agent) || preg_match('/palm/i', $agent) || preg_match('/operamini/i', $agent) | |
|| preg_match('/operamobi/i', $agent)|| preg_match('/opera mobi/i', $agent) || preg_match('/openwave/i', $agent) || preg_match('/nexusone/i', $agent) | |
|| preg_match('/cldc/i', $agent) || preg_match('/midp/i', $agent) || preg_match('/wap/i', $agent) || preg_match('/andorid/i', $agent)|| preg_match('/mobile/i', $agent)) | |
{ | |
$os = 1; | |
} | |
else{ | |
$os = 0; | |
} | |
return $os; | |
} |
阅读排行榜