Home  >  Article  >  Backend Development  >  How to use php to determine whether it is accessed by mobile phone

How to use php to determine whether it is accessed by mobile phone

藏色散人
藏色散人Original
2020-08-10 09:08:423015browse

How to determine whether it is mobile phone access in php: first check whether it is a wap proxy; then check whether the browser accepts "WML"; then check "USER_AGENT" through the "preg_match" method.

How to use php to determine whether it is accessed by mobile phone

Recommended: "PHP Video Tutorial"

PHP determines whether the user Mobile phone access

The customized function is as follows:

$agent = check_wap();
    if( $agent )
{
    header('Location: http://www.nowamagic.net');
    exit;
}
// check if wap
function check_wap(){
    // 先检查是否为wap代理,准确度高
    if(stristr($_SERVER['HTTP_VIA'],"wap")){
            return true;
        }
        // 检查浏览器是否接受 WML.
        elseif(strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0){
        return true;
   }
   //检查USER_AGENT
   elseif(preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])){
        return true;           
    }
    else{
        return false;  
   }
}

There is another function stripped from the PHP framework to determine whether it is a mobile terminal:

0)  
        return true;  
    else
        return false;  
}

The above is the detailed content of How to use php to determine whether it is accessed by mobile phone. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn