判断来自电脑还是手机

<?php
/**
*现公司在用的,不完善的地方望提出
*/
class FromPhoneOrPc {

    //判断来自电脑还是手机
    public function judgeFrom() {
        $uAgent = $_SERVER['HTTP_USER_AGENT'];

        $hAccept = $_SERVER['HTTP_ACCEPT'];

        $osPat = "mozilla|m3gate|winwap|openwave|Windows NT|Windows 3.1|95|Blackcomb|98|ME|X Window|ubuntu|Longhorn|AIX|AmigaOS|BEOS|HP-UX|OpenBSD|FreeBSD|NetBSD|OS\/2|OSF1|SUN";

        $phonePat = "iPhone|Linux|Android|Symbian|Windows Phone OS";

        if (preg_match("/($phonePat)/i", $uAgent)) {

            return 'phone';
        } else if (preg_match("/($osPat)/i", $uAgent)) {

            if (isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== FALSE) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === FALSE || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')) )) {

                return 'phone';
            } else {

                return "pc";
            }
        } else {

            return 'phone';
        }
    }

}

//1、先把现在的智能手机会显示的系统都排除掉
//
//2、再排除WAP格式的报头
//
//3、剩下的基本是PC了

 

 

posted on 2014-08-29 16:30  珩~  阅读(269)  评论(0编辑  收藏  举报

导航