2012-05-17 132 views
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检测php

到目前为止好..

现在我想插入一台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我是重定向到i-index.php文件,但不是指定的ID(?id = 1)。

我希望我的解释不要混淆。任何人都可以提出一个方法为它重定向到指定的ID(包括原来的索引的移动指数都被正确连接到数据库)

谢谢

+0

我的代码中没有看到任何'?id = 1' – zerkms

+0

请参阅关于PHP如何检测iPhone的内容。 http://stackoverflow.com/questions/10632400/iphone-detection-php – Othman

回答

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

谢谢!有用 :) – orlyidd

0

,所以我创建了一个I-指数.PHP插入

...

头( '位置: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 

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

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

我建议不要这样做在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