PHP 判断移动设备的函数isMobile()

不废话上代码,使用方法就是

 代码如下 复制代码

<?php
if(isMobile()){}
if(!isMobile()){}
 ?>

function isMobile() {

$user_agent = $_SERVER['HTTP_USER_AGENT'];
$mobile_agents = Array("240x320", "acer", "acoon", "acs-", "abacho", "ahong", "airness", "alcatel", "amoi", "android", "anywhereyougo.com", "applewebkit/525", "applewebkit/532", "asus", "audio", "au-mic", "avantogo", "becker", "benq", "bilbo", "bird", "blackberry", "blazer", "bleu", "cdm-", "compal", "coolpad", "danger", "dbtel", "dopod", "elaine", "eric", "etouch", "fly ", "fly_", "fly-", "go.web", "goodaccess", "gradiente", "grundig", "haier", "hedy", "hitachi", "htc", "huawei", "hutchison", "inno", "ipad", "ipaq", "ipod", "jbrowser", "kddi", "kgt", "kwc", "lenovo", "lg ", "lg2", "lg3", "lg4", "lg5", "lg7", "lg8", "lg9", "lg-", "lge-", "lge9", "longcos", "maemo", "mercator", "meridian", "micromax", "midp", "mini", "mitsu", "mmm", "mmp", "mobi", "mot-", "moto", "nec-", "netfront", "newgen", "nexian", "nf-browser", "nintendo", "nitro", "nokia", "nook", "novarra", "obigo", "palm", "panasonic", "pantech", "philips", "phone", "pg-", "playstation", "pocket", "pt-", "qc-", "qtek", "rover", "sagem", "sama", "samu", "sanyo", "samsung", "sch-", "scooter", "sec-", "sendo", "sgh-", "sharp", "siemens", "sie-", "softbank", "sony", "spice", "sprint", "spv", "symbian", "tablet", "talkabout", "tcl-", "teleca", "telit", "tianyu", "tim-", "toshiba", "tsm", "up.browser", "utec", "utstar", "verykool", "virgin", "vk-", "voda", "voxtel", "vx", "wap", "wellco", "wig browser", "wii", "windows ce", "wireless", "xda", "xde", "zte");

$is_mobile = false;
foreach ($mobile_agents as $device) {
  if (stristr($user_agent, $device)) {
    $is_mobile = true;
    break;
  }
}
return $is_mobile;
}

网友补充了一个

 代码如下 复制代码

<?php
function isMobile()
{
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
    {
        return true;
    }
    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER['HTTP_VIA']))
    {
        // 找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    }
    // 脑残法,判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER['HTTP_USER_AGENT']))
    {
        $clientkeywords = array ('nokia',
            'sony',
            'ericsson',
            'mot',
            'samsung',
            'htc',
            'sgh',
            'lg',
            'sharp',
            'sie-',
            'philips',
            'panasonic',
            'alcatel',
            'lenovo',
            'iphone',
            'ipod',
            'blackberry',
            'meizu',
            'android',
            'netfront',
            'symbian',
            'ucweb',
            'windowsce',
            'palm',
            'operamini',
            'operamobi',
            'openwave',
            'nexusone',
            'cldc',
            'midp',
            'wap',
            'mobile'
            );
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
        {
            return true;
        }
    }
    // 协议法,因为有可能不准确,放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT']))
    {
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
        {
            return true;
        }
    }
    return false;
}
?>

国外人喜欢写类,有一个Mobile Detect

Mobile_Detect 简单使用实例

 代码如下 复制代码

include 'Mobile_Detect.php';
$detect = new Mobile_Detect();
 
// Check for any mobile device.
if ($detect->isMobile())
 
// Check for any tablet.
if($detect->isTablet())
 
// Check for any mobile device, excluding tablets.
if ($detect->isMobile() && !$detect->isTablet())
 
if ($detect->isMobile() && !$detect->isTablet())
 
// Alternative to $detect->isAndroidOS()
$detect->is('AndroidOS');
 
// Batch usage
foreach($userAgents as $userAgent){
  $detect->setUserAgent($userAgent);
  $isMobile = $detect->isMobile();
}
 
// Version check.
$detect->version('iPad'); // 4.3 (float)

时间: 2024-07-31 20:12:43

PHP 判断移动设备的函数isMobile()的相关文章

Lua获取文件长度和判断文件是否存在函数

  这篇文章主要介绍了Lua获取文件长度和判断文件是否存在函数分享,需要的朋友可以参考下 获得文件长度 代码如下: function length_of_file(filename) local fh = assert(io.open(filename, "rb")) local len = assert(fh:seek("end")) fh:close() return len end 判断文件是否存在 代码如下: function file_exists(pat

JSP判断移动设备的正则

 天猫php判断移动设备的正则(个人猜测),觉得很好用,于是就决定移植到JSP里面,大家可以参考下 看到了一篇很好的文章, <在天猫,前端做什么?>,里面有天猫php判断移动设备的正则(个人猜测),觉得很好用,于是就决定移植到JSP里面.    jsp文件名为 index.jsp,其实也可以使用过滤器来进行拦截,然后跳转到其他域名去.    完整代码如下:  代码如下: <%@page import="java.util.regex.Matcher"%>  &l

ios-怎么判断IOS设备的版本

问题描述 怎么判断IOS设备的版本 有两个storyboard分别制定给iPhone5和iPhone4的尺寸.如何让代码检测ios版本? - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 1 UIStoryboard * mainStoryboard = nil ; if ( SYSTEM_VERSION_GREATER

android给 TextView 加上效果和事件响应 判断是否点击函数

android textView 加入连接方式: 1:使用android: 只需在textview中加入这个属性在里面写的文字中包含网址.电话.email的会自动加入连接地址. 如: <TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/text1" android:layout_width="match_parent" an

php判断类是否存在函数class_exists用法分析_php技巧

本文实例分析了php判断类是否存在函数class_exists用法.分享给大家供大家参考.具体如下: 如果我们要判断一个类是不是可以用,可以先使用class_exists函数来判断一下,下面来看几个例子. bool class_exists ( string $class_name [, bool $autoload = true ] ) 此功能是否给定的类被定义检查.this function checks whether or not the given class has been def

javascript 判断是否为日期函数代码

<html xmlns="http://www.111cn.net/ 1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>javascript 判断是否为日期函数代码</title> <script language="javas

JSP判断移动设备的正则_JSP编程

看到了一篇很好的文章, <在天猫,前端做什么?>,里面有天猫php判断移动设备的正则(个人猜测),觉得很好用,于是就决定移植到JSP里面. jsp文件名为 index.jsp,其实也可以使用过滤器来进行拦截,然后跳转到其他域名去. 完整代码如下: 复制代码 代码如下: <%@page import="java.util.regex.Matcher"%> <%@page import="java.util.regex.Pattern"%&

PHP判断移动设备来源的方法

 现在移动设备大热的年代,可能我们在做web前端开发的时候经常会需要用到对移动设备的页面匹配.当然我们可以用响应式页面设计来处理前端的匹配问题,但是响应式页面只是不显示某些代码,但是还是会加载到用户的移动设备中.为了更好的照顾到用户手机浏览的体验,减少不必要的加载.我们可以再通过PHP的方式来减少用户的代码加载,提高用户浏览页面时的体验.由于现在移动设备的种类繁多.而且浏览器五花八门,所以仅仅是通过User-Agent来判断已经不能完全解决问题了. <?php function isMobile

API之设备场景函数

CombineRgn 将两个区域组合为一个新区域 CombineTransform 驱动世界转换.它相当于依顺序进行两次转换 CreateCompatibleDC 创建一个与特定设备场景一致的内存设备场景 CreateDC 为专门设备创建设备场景 CreateEllipticRgn 创建一个椭圆 CreateEllipticRgnIndirect 创建一个内切于特定矩形的椭圆区域 CreateIC 为专用设备创建一个信息场景 CreatePolygonRgn 创建一个由一系列点围成的区域 Cre