分享一个php判断用户设备的案例

如何通过PHP判断Web客户端访问设备类型,像手机、平板、PC,这里给大家提供个轻量级的类库,Mobile Detection,专门用来检测设备的PHP类库,它主要通过检测HTTP头中的User-Agent字符串来检测移动端客户环境。注意,mobile detection 只是一个服务器端(PHP)的检测工具,并不能代替响应式Web设计或其他任何形式的客户端功能检测 

isMobile() && !$detect->isTablet()){
     // 所有手机端
     exit('手机端');
  
}else if($detect->isTablet()){
     // 所有平板设备
        echo '平板';
}else if($detect->isiOS()){
     // IOS系统
    echo 'ios';
}else if($detect->isAndroidOS()){
     // Android系统
    echo 'android';
}else if($detect->isWindowsPhoneOS()){
     //WindowsPhone系统
    echo 'WindowsPhone';
}else{
    echo 'pc端';
}

关于 Mobile_Detect:

Mobile_Detect 是一个轻量级的开源移动设备(手机)检测的 PHP Class,

它使用 User-Agent 中的字符串,并结合 HTTP Header,来检测移动设备环境。

这个设备检测的 PHP 类库最强大的地方是,它有一个非常完整的库,

可以检测出所用的设备类型(包括操作类型,以及手机品牌等都能检测)和浏览器的详细信息。

官方主页:http://mobiledetect.net/

demo:http://demo.mobiledetect.net/

js版本的:https://github.com/hgoebl/mobile-detect.js

 

你可能感兴趣的:(php)