客户端判断 PC/WEB

最近做的项目,需要在客户端PHP判断是PC端还是移动端,跟着同事学习摘录部分代码,记录如下:

function get_device_type(){
    $agent=strtolower($_SERVER['HTTP_USER_AGENT']);
    $type = 'PC';
    if(strpos($agent, 'iphone') || strpos($agent, 'ipad') ){
        $type = 'IOS' ;
    } 
    if(strpos($agent, 'android')){
        $type= 'android' ;
    }
    return $type; 
}

在页面中调用:

<?php 
    $device = get_device_type();
    if($device=="PC"){?>
        //
    <?php }else{?>    
        //
<?php }?>    

单纯判断手机端

if($device=="IOS" || $device=="android"){}

 

posted @ 2017-08-31 16:38  雲淡颩淸  阅读(150)  评论(0编辑  收藏  举报