+44 (791) 5698 086

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.

31 Responses to Redirect user to iPhone / iPod version of site using PHP

  1. Abby Eagle

    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.

    [code]
    $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”);
    [/code]

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

    • Nabeel

      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.

    • Nabeel

      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:

      < ?php
      
      //setting the iPhone / iPod browser variables
      $ipod = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
      $iphone = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
      $android = stripos($_SERVER['HTTP_USER_AGENT'],"android");
      
      //detecting device browser
      if ($ipod == true || $iphone == true || $android == true){
      header( 'Location: http://m.domain.com/' ) ;
      }
      
      ?>

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

      • suzanne

        Hi,

        if i have code like below ,

        session_start();
        check_logged();

        if( $logged[‘member’] ){
        header(‘Location: http://www.mysite.com/index.php‘);

        should i put my script before or after your script and

        one more thing, do your script must be on top ?

        • Nabeel

          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. Abby

    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.

    • Nabeel

      ok.. so did the code work for you?

  3. Abby

    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.

    • Nabeel

      You’re welcome Abby

  4. Lladro figurines

    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. miss suzy

    hi, if i have codes like below
    [code][/code]

    should i put after or before this code

    [code][/code]

    • Nabeel

      sorry I didn’t receive the code, :s please send to me again without including javascript start and end tag

  6. miss suzy

    hi, if i have codes like below

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

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

    should i put after or before this code

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

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

    • Nabeel

      can you please tell me your browser? and details of how you tried to paste the code, this will help me fix this issue of code in comments. Thanks

  7. saysa

    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

    • Nabeel

      you can do this by getting the ip address of the user and then tracing the location of that ip

  8. Alex

    What if i want it to change something on the page instead of a redirect?

    • Nabeel

      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

      • Alex

        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

        • Nabeel

          thats pretty simple, just detect the browser and make a condition for which code to echo!

          • Alex

            ugh … noob mistake.. thanks!

          • Nabeel

            You’re welcome Alex! :) Let me know if you need any assistance

Leave a Reply

Your email address will not be published. Required fields are marked *

Who are we?

Nabtron team includes web, desktop software and mobile apps developers along with SEO professionals.

We are available 24 hours, 7 days a week. Contact us now for anything you need!

Join our mailing list

Lets become friend :) What's your name & email?