Jump to content

Web redirection to iPhone viewers

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
Just wanted to drop by and seek some help. Does anyone know a PHP code that the website detects the iPhone handset and redirects to the mobile viewable website?

More information (http://forum.codecal...sion-doing.html)

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
A most simple implementation would be to get the user agent of the device or computer (through the PHP variable $_SERVER['HTTP_USER_AGENT']) and to compare if the string "iPhone" or "iPod" exists. It is almost guarenteed for that string to exist in the user agent so this should work fine placing it top of your webpage and renaming index.html to index.php:
<?php
if(strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'], 'iPod'))
{
    header('Location: http://yoursite.com/iphone'); //replace with anything you wish after Location: 
    exit();
}
?>
This would need to be placed at the very first part of your page, with no whitespace (including a newline) behind the <?php, and it should redirect the user. Are you able to work with PHP files, do you have a web server running where you can test it?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts

Nullw0rm said:

A most simple implementation would be to get the user agent of the device or computer (through the PHP variable $_SERVER['HTTP_USER_AGENT']) and to compare if the string "iPhone" or "iPod" exists. It is almost guarenteed for that string to exist in the user agent so this should work fine placing it top of your webpage and renaming index.html to index.php:
<?php
if(strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'], 'iPod'))
{
header('Location: http://yoursite.com/iphone'); //replace with anything you wish after Location: 
exit();
}
?>
This would need to be placed at the very first part of your page, with no whitespace (including a newline) behind the <?php, and it should redirect the user. Are you able to work with PHP files, do you have a web server running where you can test it?

My page is index.php and runs a couple of php sections within the html -
however I tried similar code as you gave me, and it doesn't work.
I was trying to redirect it to http://m.canberradeafclub.org.au'
however I'll give it another go but where excatly would it supposed to sit?

(have to do it soon as my website just crashed recently)

gotta go..!

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
It should not matter what is in your index.php, you can safely put that at the absolute first part of the page and it will run. This is a very basic command, it will work or display an error if it fails somehow.

If you are unsure if this would work, you can always replace it with this and run it on your ipod/iphone to prove it is within the useragent string:
<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
The php redirection unfortunately doesn't work?
Neither does mine - it should redirect to my mobile (iphone version) website.

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
You are most likely setting it up improperly, all iPhone default browsers will display the user agent and the function should match the string within.

How is the first part of the script set up?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.