3

I have several Index.php files, such as (index.php, index2.php, index3.php etc.), I also have a database with different ID URL's. when some one enters a index file, the landing pages the see are dynamic and only the specified parameters in the database changes some of the parameters on display, lets say i enter domain.com/index.php?id=2 - this is for X city and the domain domain.com/index2.php?id=112 will display a page for Y city.

So far so Good..

now i am trying to insert a Iphone detection code, that will redirect users that are entering the urls from iphone to an iphone friendly design. so i created an i-index.php inserted the following code into the index.php page:

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

and now when i enter the url mydomain.com/index.php?id=1 from my iphone i am redirected to the i-index.php file but not to the specified ID (?id=1).

i hope my explanation is not to confusing. can anyone suggest a way for it to redirect to the specified ID (both original index and the mobile index are connected to the database correctly)

Thank you

orlyidd
  • 31
  • 3
  • 5

6 Answers6

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();
}
?>
Menztrual
  • 40,867
  • 12
  • 57
  • 70
0

so i created an i-index.php inserted

...

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

So if an iphone requests i-index.php, that script sends a redirect to i-index.php

?

not to the specified ID (?id=1).

Where does it say '?id=1' in your code?

symcbean
  • 47,736
  • 6
  • 59
  • 94
0
header('Location: http://www.mydomain.com/mobile/i-index.php?id='.$_GET['id']);
pre
  • 3,475
  • 2
  • 28
  • 43
0

Just Keep searching in the web

<?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

?>

and in javascript you say

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

Community
  • 1
  • 1
Othman
  • 2,942
  • 3
  • 22
  • 31
0

This will add the whole query string to the i-index.php. So any querystring you receive will be passed to the i-Phone version also. This is good for future changes if you need to add some more parameter to index.php you don't have to change this code again.

<?

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();
}
?>
Imdad
  • 5,942
  • 4
  • 33
  • 53
0

I suggest not doing this in PHP but using RewriteEngine in a .htaccess file. This might look something like that:

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

The QSA is taking care of the query string and adding the original parameters to the new url and therefore gives you more flexibility.

There is also a more sophisticated version available in a different thread.

Community
  • 1
  • 1
sprain
  • 7,552
  • 6
  • 35
  • 49