我得到了一个运行良好的函数,但我需要对它进行一点扩展,我不是程序员。
现在它可以很好地检测ios,并且android和web都被重定向到一个页面,用于web和android。
目标是将ios重定向到第1页,将android重定向到第2页,将web操作系统重定向到第3页。
function find_mobile_browser() {
if(preg_match('/(iphone|ipad|ipod)/i', $_SERVER['HTTP_USER_AGENT'])) {
return true;
} else {
return false;
}
}
电话是这样的:
<?php $mobile_browser = find_mobile_browser();
if($mobile_browser) {
include("ios.php"); /* if mobile browser detected, do this */
} else {
include("android_and_web.php"); /* else do this */
}
?>
你能帮我扩展这个片段来检测它吗,这样调用就可以这样:
<?php $mobile_browser = find_mobile_browser();
if($mobile_browser) {
include("ios.php"); /* if ios detected, do this */
} else {
include("android.php"); /* if android detected, do this */
}
} else {
include("web_os.php"); /* if web-os detected, do this */
}
?>
我认为这不是检测移动设备的完美方法,但有更好的方法吗?
谢谢,奥威尔
在你回答得很好之后,我会解释得更好。
"头"的答案对我来说很有用,但我不是程序员,所以我不想让它按我想要的方式工作。重定向工作得很好,但我不需要重定向,我需要include。我想在我的一些模板中包含一些依赖ios/android/web的文本片段(php文件)。
这个函数运行得很好,但我的调用没有显示任何结果。在这里,我尝试调用函数:
<?php
$mobile_browser = find_mobile_browser();
if($mobile_browser == 'ios')
{
include("ios.php");
}
elseif ($mobile_browser == 'android')
{
include("android.php");
}
else
{
/* if no mobile os detected, include "web.php" */
}
?>
希望我现在更清楚了,你不要难过。
提前感谢,奥威尔
谢谢你的回答,-我现在明白了:)
干杯,奥威尔
使用Mobile Detect php类。
示例:
<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
if($detect->isAndroidOS()) {
include("android.php");
}elseif( $detect->isiOS()) {
include("ios.php");
}else( $detect->iswebOS()) {
include("web_os.php");
}
?>
为了重定向,您可以使用头
header('Location:'.$your_url);
因此,代码将变成类似(取决于$mobile_browser
的值)
function find_mobile_browser()
{
if(preg_match('/(iphone|ipad|ipod)/i', $_SERVER['HTTP_USER_AGENT']))
{
return 'ios';
}
elseif (preg_match('/(android)/i', $_SERVER['HTTP_USER_AGENT']))
{
return 'android';
}
else
{
return false;
}
}
<?php
$mobile_browser = find_mobile_browser();
$ios_url = 'http://www.example.com';
$android_url = 'http://www.example.com';
$web_url = 'http://www.example.com';
if($mobile_browser == 'ios')
{
header('Location:'.$ios_url);
exit;
}
elseif ($mobile_browser == 'android')
{
header('Location:'.$android_url);
exit;
}
else
{
header('Location:'.$web_url);
exit;
}
?>
您可以使用名为:Mobile Detect 的PHP类
下载链接:https://github.com/serbanghita/Mobile-Detect/archive/2.8.12.zip
以下是使用它的示例片段,您可以根据需要进行自定义:
<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) {
}
// Any tablet device.
if( $detect->isTablet() ){
}
// Exclude tablets.
if( $detect->isMobile() && !$detect->isTablet() ){
}
// Check for a specific platform with the help of the magic methods:
if( $detect->isiOS() ){
}
if( $detect->isAndroidOS() ){
}
$detect->is('Chrome');
$detect->is('iOS');
$detect->is('UC Browser');
$userAgents = array(
'Mozilla/5.0 (Linux; Android 4.0.4; Desire HD Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
'BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103',
);
foreach($userAgents as $userAgent){
$detect->setUserAgent($userAgent);
$isMobile = $detect->isMobile();
$isTablet = $detect->isTablet();
}
$detect->version('iPad'); // 4.3 (float)
$detect->version('iPhone') // 3.1 (float)
$detect->version('Android'); // 2.1 (float)
$detect->version('Opera Mini'); // 5.0 (float)
?>