php检测设备类型

Posted 老白个人博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php检测设备类型相关的知识,希望对你有一定的参考价值。

<?php
//检测设备类型
function userAgent(){
## This credit must stay intact (Unless you have a deal with @lukasmig or frimerlukas@gmail.com
## Made by Lukas Frimer Tholander from Made In Osted Webdesign.
## Price will be $2
     $ua = $_SERVER[‘HTTP_USER_AGENT‘];
    $iphone = strstr(strtolower($ua), ‘mobile‘); //Search for ‘mobile‘ in user-agent (iPhone have that)
    $android = strstr(strtolower($ua), ‘android‘); //Search for ‘android‘ in user-agent
    $windowsPhone = strstr(strtolower($ua), ‘phone‘); //Search for ‘phone‘ in user-agent (Windows Phone uses that)
     
     
    function androidTablet($ua){ //Find out if it is a tablet
        if(strstr(strtolower($ua), ‘android‘) ){//Search for android in user-agent
            if(!strstr(strtolower($ua), ‘mobile‘)){ //If there is no ‘‘mobile‘ in user-agent (Android have that on their phones, but not tablets)
                return true;
            }
        }
    }
    $androidTablet = androidTablet($ua); //Do androidTablet function
    $ipad = strstr(strtolower($ua), ‘ipad‘); //Search for iPad in user-agent
     
    if($androidTablet || $ipad){ //If it‘s a tablet (iPad / Android)
        return 0.75;
    }
    elseif($iphone && !$ipad || $android && !$androidTablet || $windowsPhone){ //If it‘s a phone and NOT a tablet
        //return ‘mobile‘;
        return 0.5;
    }else{ //If it‘s not a mobile device
        //return ‘desktop‘;
         return 1.0;
    }   
}
 
?>
<meta http-equiv="Content-type" name="viewport" content="initial-scale=<?php echo userAgent();?>, maximum-scale=1.0, user-scalable=no, width=device-width">

以上是关于php检测设备类型的主要内容,如果未能解决你的问题,请参考以下文章

使用 PHP 检测和删除空间类型 [重复]

为啥 php 不能正确检测到 pdf mime 类型(wkhtmltopdf)?

php基础:变量检测

PHP 检测Joomla中的页面类型

一些不常用的PHP字符类型检测函数

Mime 类型检测在 PHP 5.3.8 上使用 fileinfo 失败