本文介绍了检测手机或平板电脑设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说,目标是建立一个基于Wordpress的移动网站(用于手机和平板电脑)和响应式桌面网站。我想要实现傻瓜式设备检测的最简单方法。

The aim for me is to have a mobile website (for mobiles and tablets) and a responsive desktop website, built on Wordpress. I want the easiest way to implement fool proof device detection.

移动网站将具有很多功能,这些功能只会真正使触摸设备受益,并且将会专为手机和平板电脑设计。桌面站点将完全不同(具有相同页面,但具有附加内容),并且将完全响应,以防万一任何设备通过检测。

The mobile website is going to have a lot of features that are only really going to benefit touch devices and will be custom designed for mobiles and tablets. The desktop site will be completely different (with same pages but with additional content) and will be fully responsive just in case any devices slip through the detection.

我有这个可以检测触摸设备并重定向到另一页的衬里,但是看起来太简单了,不能作为一种简单的设备检测方法。这是多么愚蠢的证明?它适用于Windows和Android设备以及iOS吗?

I have this one liner that will detect touch devices and redirect to another page but it seems too simple to be a fool proof method of device detection. How fool proof is this? Will it work for Windows and Android devices as well as iOS?

这是我第一次做这样的网站,不确定这将是多么有效:

This is my first time doing such a site and not sure how effective this is going to be:

<!-- Redirect to the mobile index page if we're on a touch device -->
<script>if( 'ontouchstart' in window ) window.location = 'mobile.html';</script>


推荐答案

对于PHP上的移动检测,我一直使用$ _SERVER ['HTTP_USER_AGENT']变量。
我使用下面的功能对谁提供了移动网站(我想支持的特定设备)进行了非常精细的控制。只需将用户代理添加到阵列(例如 Ipad)即可添加其他设备。

For Mobile Detection on PHP I have always used the $_SERVER['HTTP_USER_AGENT'] variable.I use the function below to have very granular control over who gets delivered the mobile site (specific devices I want to support). Simply add useragents to the array (such as 'Ipad') to add additional devices.

function detectmobile(){
    $agent = $_SERVER['HTTP_USER_AGENT'];
    $useragents = array (
        "iPhone",
        "iPod",
        "Android",
        "blackberry9500",
        "blackberry9530",
        "blackberry9520",
        "blackberry9550",
        "blackberry9800",
        "webOS"
        );
        $result = false;
    foreach ( $useragents as $useragent ) {
    if (preg_match("/".$useragent."/i",$agent)){
            $result = true;
        }
    }
return $result;
}
if (detectmobile() == true) { wp_redirect('http://pageyouwwanttoredirectto'); }

用法:您可以将以上代码添加到wordpress主题的functions.php文件中。

Usage: you can the above code to your wordpress theme's functions.php file.

这篇关于检测手机或平板电脑设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 21:17