如何确定设备是移动设备还是台式机? [英] How to determine if the device is Mobile or Desktop?

查看:82
本文介绍了如何确定设备是移动设备还是台式机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多文章,并在寻找用于检测移动设备的解决方案.实际上遇到了 https://github.com/serbanghita/mobile-detect 大量的php类.

I have read a lot of articles and looked for solutions to detect mobile devices. Actually a came across https://github.com/serbanghita/mobile-detect but it's a quite massive php class.

我实际上想要一个非常简单的解决方案.我想确定用户的浏览器是Mobile/iPad/etc还是Desktop.所以我想要这样的东西:

I actually want a very simple solution. I want to determine if the user's browser is Mobile/iPad/etc OR Desktop. So I want something like this:

<?php

require('detector.php');

if(isMobile() === true)
{
   header('mobile.php');
   exit();
}
else
{
   header('desktop.php');
   exit();
}

?>

需要一个非常简单的解决方案,无需安装作曲家或任何php框架即可将其放置在任何页面上.

A very simple solution is needed which I can place to any page without installing composer or any php framework.

实际上这怎么可能?

推荐答案

您实际上是否尝试过使用发现的项目.我会说服务器端移动检测是一项艰巨的任务,需要进行大量的详细检查以确保获得正确的结果.

Have you actually tried to use the project you discovered. I'd say that server side mobile detection IS a huge task with plenty of detail checks to ensure the correct outcome.

使用此类非常简单.在示例目录中:

And using this class is completely easy. From the example directory:

require_once '../Mobile_Detect.php';
$detect = new Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');

现在,您有了一个具有以下三个值之一的变量:平板电脑",电话"或计算机",您可以对此做出反应.

Now you have a variable with one of three values: "tablet", "phone" or "computer", and you can react to this.

请注意,即使您可以在不使用Composer的情况下使用此库,也会定期对其进行更新(如每月一次"),因为新设备已投放市场并需要进行检测.您将必须在某个时候更新此库.使用Composer可以使此操作非常容易.

Please note that even if you are able to use this library without Composer, it will be updated regularly (as in "once every month"), because new devices get on the market and need to be detected. You will have to update this library at some point. Using Composer makes this very easy.

这篇关于如何确定设备是移动设备还是台式机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆