我有几个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输入URL mydomain.com/index.php?id=1 时,我被重定向到i-index.php文件,而不是指定的ID(?id=1(。
我希望我的解释不会令人困惑。 谁能建议一种重定向到指定 ID 的方法(原始索引和移动索引都正确连接到数据库(
谢谢
<?
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();
}
?>
所以我创建了一个 i-index.php插入
。
标头("位置:http://www.mydomain.com/mobile/i-index.php"(;
因此,如果iPhone请求i-index.php,该脚本会向i-index发送重定向.php
?
不是指定的 ID (?id=1(。
代码中哪里说"?id=1"?
header('Location: http://www.mydomain.com/mobile/i-index.php?id='.$_GET['id']);
继续在网络上搜索
<?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';
}
检测苹果浏览器
这会将整个查询字符串添加到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();
}
?>
我建议不要在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,因此为您提供了更大的灵活性。
在不同的线程中还有一个更复杂的版本。