【问题标题】:Iphone detection phpiphone检测php
【发布时间】:2012-05-17 08:44:55
【问题描述】:

我有几个 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 网址的用户重定向到对 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 输入 url mydomain.com/index.php?id=1 时,我被重定向到 i-index.php 文件,而不是指定的 ID (?id=1)。

我希望我的解释不会令人困惑。谁能建议一种方法让它重定向到指定的ID(原始索引和移动索引都正确连接到数据库)

谢谢

【问题讨论】:

标签: php iphone redirect detection


【解决方案1】:
<?
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();
}
?>

【讨论】:

    【解决方案2】:

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

    ...

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

    因此,如果 iphone 请求 i-index.php,该脚本将重定向到 i-index.php

    ?

    不是指定的 ID (?id=1)。

    在您的代码中,它在哪里显示“?id=1”?

    【讨论】:

      【解决方案3】:
      header('Location: http://www.mydomain.com/mobile/i-index.php?id='.$_GET['id']);
      

      【讨论】:

        【解决方案4】:

        继续在网上搜索

        <?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';
        }
        

        Detect iPhone Browser

        【讨论】:

          【解决方案5】:

          这会将整个查询字符串添加到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();
          }
          ?>
          

          【讨论】:

            【解决方案6】:

            我建议不要在 PHP 中这样做,而是在 using RewriteEngine in a .htaccess file 中这样做。 这可能看起来像这样:

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

            QSA 负责处理查询字符串并将原始参数添加到新 url,因此为您提供了更大的灵活性。

            还有一个更复杂的版本in a different thread

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2023-01-18
              • 1970-01-01
              • 1970-01-01
              • 2013-06-06
              • 1970-01-01
              • 2011-04-19
              • 1970-01-01
              • 2012-04-15
              相关资源
              最近更新 更多