Site icon Nabtron

Redirect user to iPhone / iPod version of site using PHP

Previously i wrote an article which explains Detect users internet browser and redirect to different page specific to it but this was just a basic idea on how to do this focusing mainly on detecting the user browser.

In this tutorial you will be learning how to detect if the user is coming from Apple iPhone or iPod and redirect him to the mobile version of your website. We will also see how to move him to normal domain if he comes from non-iPhone (and non-iPod) browser to the mobile version of the site.

We will use the redirect codes in PHP along with browser detection to make our logic work.

Scenario:

We have a website named: domain.com which has a subdomain: m.domain.com which hosts the mobile version of our site. What we want to do is that if some one comes through an iPhone or iPod on our site we want him to be redirected to m.domain.com using PHP.

Code:

Here is the code that will go on top of the domain.com/index.php file:

<?php

//setting the iPhone / iPod browser variables
$ipod = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iphone = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");

//detecting device browser
if ($ipod == true || $iphone == true){
header( 'Location: http://m.domain.com/' ) ;
}
?>

Please note use this code on top of domain.com/index.php ONLY and NOT on m.domain.com/index.php

If you have any queries or specific requirements for this code please tell me in comments i will update it according to your needs.

Exit mobile version