iPhone检测PHP

3
我有几个Index.php文件,比如(index.php,index2.php,index3.php等),我还有一个具有不同ID URL的数据库。当有人输入索引文件时,他们看到的着陆页面是动态的,只有数据库中指定的参数改变了一些显示参数。比方说,我输入domain.com/index.php?id=2 - 这是为X城市设计的,而domain.com/index2.php?id=112将显示Y城市的页面。
到目前为止还不错...
现在我正在尝试插入一个iPhone检测代码,该代码将重定向从iPhone输入URL的用户到一个iPhone友好的设计。所以我创建了一个i-index.php,并将以下代码插入到index.php页面中:
<?
if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod'))
{
  header('Location: http://www.mydomain.com/mobile/i-index.php');
  exit();
}
?>

现在,当我从我的 iPhone 输入mydomain.com/index.php?id=1 的 URL 时,我被重定向到 i-index.php 文件,但没有重定向到指定的 ID (?id=1)。我希望我的解释不会让人困惑。有人能建议一种方法使它重定向到指定的 ID 吗?(原始索引和移动索引都正确连接到数据库)谢谢。

我在你的代码中没有看到任何 ?id=1 - zerkms
看看这个关于PHP如何检测iPhone的链接。https://dev59.com/sWPVa4cB1Zd3GeqP-fBj - Othman
6个回答

2
<?
if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod'))
{
  header('Location: http://www.mydomain.com/mobile/i-index.php?id='.$_GET['id']);
  exit();
}
?>

0

继续在网上搜索

<?php

function isIphone($user_agent=NULL) {
    if(!isset($user_agent)) {
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    }
    return (strpos($user_agent, 'iPhone') !== FALSE);
}

if(isIphone()) {
    header('Location: http://www.yourwebsite.com/phone');
    exit();
}

// ...THE REST OF YOUR CODE HERE

?>

而在 JavaScript 中,你会说

var agent = navigator.userAgent;
var isIphone = ((agent.indexOf('iPhone') != -1) || (agent.userAgent.indexOf('iPod') != -1)) ;
if (isIphone) {
    window.location.href = 'http://www.yourwebsite.com/phone';
}

检测 iPhone 浏览器


0

这将把整个查询字符串添加到 i-index.php 中。因此,您收到的任何查询字符串也将传递给 i-Phone 版本。如果您需要向 index.php 添加更多参数以进行未来更改,则此方法非常好,因为您不必再次更改此代码。

<?

if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod'))
{
  header('Location: http://www.mydomain.com/mobile/i-index.php?' . $_SERVER['QUERY_STRING']);
  exit();
}
?>

0
所以我创建了一个i-index.php插入。

...

header('Location: http://www.mydomain.com/mobile/i-index.php');

如果 iPhone 请求 i-index.php,那么该脚本将发送重定向到 i-index.php。

?

“不是指定的ID(?id=1)。”
“你的代码中哪里写到了‘?id=1’?”

0
header('Location: http://www.mydomain.com/mobile/i-index.php?id='.$_GET['id']);

0

我建议不要在 PHP 中这样做,而是使用 .htaccess 文件中的 RewriteEngine。可能会像这样:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteCond %{REQUEST_URI} !^/mobile/i-index.php
RewriteRule .* /mobile/i-index.php [R,QSA]

QSA 负责处理查询字符串并将原始参数添加到新的 URL 中,因此可以提供更多的灵活性。

还有一个更复杂的版本可在另一个线程中找到。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接