php怎么判断是电脑端还是手机端

写一个函数is_mobile(),用来判断当前是电脑端还是手机端打开,这样就可以针对不同的设备做不同的处理。

<?php
function is_mobile(){
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $is_pc = (strpos($agent, 'windows nt')) ? true : false;
        $is_mac = (strpos($agent, 'mac os')) ? true : false;
        $is_iphone = (strpos($agent, 'iphone')) ? true : false;
        $is_android = (strpos($agent, 'android')) ? true : false;
        $is_ipad = (strpos($agent, 'ipad')) ? true : false;        
        if($is_pc){              
            return  false;
        }        
        if($is_mac && !$is_iphone){              
           return  false;
        }        

        if($is_iphone){              
            return  true;
        }        

        if($is_android){              
            return  true;
        }        
        if($is_ipad){              
            return  true;
        }

}

if(is_mobile()){
    echo '手机端';
}else{
    echo '电脑端';
}

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: