php 检测移动设备 - 和平板设备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9982907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:01:15  来源:igfitidea点击:

Detect mobile devices - and tablet devices

phpmobiledevice-detection

提问by denlau

I am currently looking at some code for PHP detection of mobiles, which is probably quite easy.

我目前正在查看一些用于 PHP 检测手机的代码,这可能很容易。

Now I just have got one problem - I want to make it possible to make unique view-files in my MVC-framework for tablets, mobiles and web pages. So I need to split the tablet from the rest of the mobile devices.

现在我遇到了一个问题——我想让在我的 MVC 框架中为平板电脑、手机和网页制作独特的视图文件成为可能。所以我需要将平板电脑与其他移动设备分开。

Currently this is the code, that I am using:

目前这是我正在使用的代码:

public function isMobile()
{   
    if(preg_match('/(alcatel|amoi|android|avantgo|blackberry|benq|cell|cricket|docomo|elaine|htc|iemobile|iphone|ipad|ipaq|ipod|j2me|java|midp|mini|mmp|mobi|motorola|nec-|nokia|palm|panasonic|philips|phone|sagem|sharp|sie-|smartphone|sony|symbian|t-mobile|telus|up\.browser|up\.link|vodafone|wap|webos|wireless|xda|xoom|zte)/i', $_SERVER['HTTP_USER_AGENT']))
    return true;
else
    return false;
}

But this is not enough - the only check is wheter the device is a mobile device - if not it is as standard telling the framework, that we're on a computer. The last part is ok - but I want to make a split of the mobile devices in actual mobiles - and in a secound group, which should be tablets.

但这还不够 - 唯一的检查是设备是否是移动设备 - 如果不是,则作为标准告诉框架,我们在计算机上。最后一部分没问题 - 但我想在实际手机中拆分移动设备 - 并在第二组中,应该是平板电脑。

I hope, that I have made my wish clear, and I hope, that you have some input in a good way to achieve this.

我希望,我已经表达了我的愿望,我希望你能以一种好的方式来实现这一目标。

Thanks in advance.

提前致谢。

回答by Joe

Here is a class with methods for detecting each platform individually.

这是一个包含单独检测每个平台的方法的类。

回答by Alexandru Guzinschi

Old question, but here goes my opinion regarding mobile detection.

老问题,但这是我对移动检测的看法。

You state in your question that you want unique files for mobile devices, so I can assume the reason for this is to present a different version of the website for mobile clients and desktop clients.

您在问题中声明您想要移动设备的唯一文件,因此我可以假设这样做的原因是为移动客户端和桌面客户端提供不同版本的网站。

This approach is OK until a certain point. And that point is called Android. There are ~1.5 millionAndroid devices activated each day with resolutions from 320*240 to 2560*1600, which makes it hard to if {} elsefor each of them. Even if you try to make a list with most used devices and you try to target only those, it will be hard to support a new device in the future.

这种方法在某一点之前是可以的。这一点被称为Android。每天有大约 150 万台Android 设备被激活,分辨率从 320*240 到 2560*1600,这使得if {} else每个设备都难以启动。即使您尝试列出最常用的设备并尝试仅针对这些设备,将来也很难支持新设备。

My approach a while back was to forget about old method of splitting devices into "mobile" and "desktop" categories and create a new method. And that method consist in "good" and "bad" browsers which is based on browser capabilities. For example, if the browser supports local storage, it will be in "good" category.

不久前我的方法是忘记将设备分为“移动”和“桌面”类别的旧方法并创建一种新方法。该方法包括基于浏览器功能的“好”和“坏”浏览器。例如,如果浏览器支持本地存储,它将在“良好”类别中。

Starting from this, I had the possibility to create a "base" version of the website, very basic from a UI standpoint but which will work cross-browser. This base version of the website will present the same content (because that matters at the end of the day) on all devices, will be very small in size (less assets, smaller html) and based on browser capabilities will be enriched on the client side.

从这里开始,我有可能创建网站的“基本”版本,从 UI 的角度来看非常基本,但可以跨浏览器工作。该网站的基本版本将在所有设备上呈现相同的内容(因为这在一天结束时很重要),尺寸非常小(更少的资产,更小的 html)并且基于浏览器的功能将在客户端上得到丰富边。

So in the end you will end up with a website that has very small footprint (html size and assets), that looks OK cross browser and it will support any new device that comes up on the market without any changes, will load fast even on poor connections and that can be enriched on client side based on browser capabilities.

因此,最终您将得到一个占用空间非常小的网站(html 大小和资产),跨浏览器看起来不错,它将支持市场上出现的任何新设备而无需任何更改,即使在连接不良,可以根据浏览器功能在客户端进行丰富。

You can even enrich the webpage based on devices size: if the browser reports a large screen, you can bring in more assets, more ads and make the webpage more beautiful ; if the browser reports is on a small screen, you leave it as is.

甚至可以根据设备大小丰富网页:如果浏览器报告大屏,可以引入更多资产,更多广告,使网页更美观;如果浏览器报告显示在小屏幕上,则保持原样。

回答by Hymansonfdam

Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment. — Read more http://mobiledetect.net

Mobile_Detect 是一个轻量级的 PHP 类,用于检测移动设备(包括平板电脑)。它使用结合特定 HTTP 标头的 User-Agent 字符串来检测移动环境。— 阅读更多 http://mobiledetect.net

回答by SteveK

You can also use a more comprehensive solution like WURFL Cloud, which is a service that detects the capabilities of mobile devices, like is_mobile and is_tablet. There is a free plan for low traffic sites: http://www.scientiamobile.com/cloud

您还可以使用更全面的解决方案,例如 WURFL Cloud,它是一种检测移动设备功能的服务,例如 is_mobile 和 is_tablet。低流量网站有免费计划:http: //www.scientiamobile.com/cloud