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.

hire best wordpress php css developer
Need Help? Hire me now!
I can help you in anything related to webdevelopment, including: HTML, CSS, PHP & MySql, WordPress (plugins & themes development, others), regular expressions, server management, etc.

23 comments on “Redirect user to iPhone / iPod version of site using PHP

  1. Hi Nabeel, that code works perfectly. Now i am wondering how to expand the list of mobile devices? Would the following list work? Not sure about the spaces.

    $ipod = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
    $iphone = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $iphone = stripos($_SERVER['HTTP_USER_AGENT'],"android");
    $iphone = stripos($_SERVER['HTTP_USER_AGENT'],"opera mini");
    $iphone = stripos($_SERVER['HTTP_USER_AGENT'],"blackberry");
    $iphone = stripos($_SERVER['HTTP_USER_AGENT'],"palm os");
    $iphone = stripos($_SERVER['HTTP_USER_AGENT'],"windows mobile");
    

    Would it be more elegant to use an include file with a list of mobile devices?

    1. Looks like you are new to PHP . . .

      do you want my service to do it live on the site you are working on? Is this your own site or some project that you are working on?

      if you can wait for a while i can upload code for multiple browsers too btw.

    2. In your case you need to specify each mobile device with it’s own variable name (as re defining the variable clears the previous value and you can’t add it to the previous one in this code logic case – i will post a different one soon too btw)

      and make sure you know the correct http_user_agent value for each mobile browser.

      You can have this example for your case: if you want to add android to it then use:

      Hopefully it makes sense. Do let me know if you need any further assistance.

        1. my script should be on the top

          why? because it will check if user is coming from iphone or not, once it is, there is no purpose to stay on this page, and he will be taken to iphone optimized page

          if he is not, then run the rest of the code, which is your’s code.

          if you need further help, do let me know

  2. Hi Nabeel, yes, new to php and very tired… need a holiday… how did i miss updating the variables? ouch! This is my site that i am working on… and don’t have a lot of cash to pay tech support so i try and work things out for myself. I am very grateful for your help.

  3. Hi Nabeel, as i said before the code works marvelously. I don’t have a mobile device but i can test by changing the user agent in the web browser, and it redirects the page to the mobile subdomain. I tried lots of other code (that worked for others) but yours was the first code that actually worked for me. Once again many thanks.

  4. We created an iPod-style drilldown menu to help users traverse hierarchical data quickly and with control. It’s especially helpful when organizing large data structures that don’t translate well into traditional dropdown or fly-out menus.

  5. hi, if i have codes like below

    ——————————————————-

    ——————————————————-

    should i put after or before this code

    ——————————————————-

    ——————————————————-

  6. Hellooo every one

    plz can anyone guide me that can we check the settings of the mobile set of user opening the site? i want to check the location settings of mobile set, can i do it in my php site?

    reply ASAP
    thanks in adv.

    Regards

    1. you can use the same code to detect the browser and change, add, edit the part of code accordingly.

      Tell me the details i might try to help

      1. well currently im loading a short video on my home page that i’d like to replace with an image instead on iphone and other small form factor mobile devices.

        check it out on wmlm.co

Leave a Reply to Nabeel Cancel reply

Your email address will not be published.