首页 > 计算机技术 > 后端开发 > PHP

PHP语言中如何判断请求是来自电脑还是手机的方法

原创 lihf8515 2025年02月20日 20:10
来源:本站 阅读:494

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;
}
作者
昵称:君子如兰
注册时间:2025-01-31 02:47:34
最近更新:2026-01-24 17:01:06
文章总数:94篇
原创 88篇   转载 6篇
友情链接: 海峰收银系统  
Copyright © 2025 hfsoft.top All Rights Reserved.
中华人民共和国工业和信息化部ICP备案序号:皖ICP备2025073039号