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)
Web redirection to iPhone viewers
Started by jclarke, Nov 14 2010 08:09 PM
5 replies to this topic
#1
Posted 14 November 2010 - 08:09 PM
|
|
|
#2
Posted 14 November 2010 - 08:27 PM
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.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 14 November 2010 - 08:50 PM
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
Posted 14 November 2010 - 08:59 PM
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:
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.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#5
Posted 15 November 2010 - 06:55 PM
The php redirection unfortunately doesn't work?
Neither does mine - it should redirect to my mobile (iphone version) website.
Neither does mine - it should redirect to my mobile (iphone version) website.
#6
Posted 15 November 2010 - 11:38 PM
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?
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.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.


Sign In
Create Account


Back to top









