Feb
06
2009

PHP Code to Redirect Mobile/Handheld Users

by   |  Posted in Tutorials  |  22 comments

With the amount of people walking around using iPhones or some sort of Blackberry or PDA, it makes sense to provide an alternative landing page for Mobile/Handheld users that is formatted for their device. I was recently hired to create a mobile page by a client and found a great piece of code to help with the whole redirection issue.

The folks over at Detect Mobile Browsers have developed a great tool that checked to see if a user has reached your site through a Mobile/Handheld device, and if so will redirect to the page your choice.

Just download the code off of their site and use their handy Function Generator to configure it how you want.

To configured it to treat everything as a Mobile/Handheld device use this code.

<?php
include('mobile_device_detect.php');
mobile_device_detect(true,true,true,true,'http://your-site.com/your-site-mobile-page',false);
?>

To add an if statement to only make it redirect if it is the home page use this.

<?php
if(is_home()) {
include('mobile_device_detect.php');
mobile_device_detect(true,true,true,true,'http://your-site.com/your-site-mobile-page',false);
}
?>

Add this to the top of your header.php file and uploaded their mobile_device_detect.php file to your main directory. If you are not using WordPress, just add the first bit of code above (without the if statement) to the top of your index.php file.

NOTE: This will only work if your site is built with PHP.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
Share the love...

Tags: , , , , , , , , , , , ,

Short URL: http://bit.ly/92bQnE

Discussion 22 Comments

  1. Idel on May 8, 2009 at 8:32 am

    Hi,
    try to use Apache Mobile Filter, is very useful to manage mobile device access, it’s based on WURFL as device repository.
    For more info: http://www.idelfuschini.it/apache-mobile-filter.html

  2. Idel on May 19, 2009 at 10:55 am

    I have published the new version of Apache Mobile Filter, now the filter is give to you the information of capabilities as apache environment.
    Now you can develope in any language (php,jsp, ruby etc.) and have the information of mobile capability.

    Read more info here: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html

  3. Jeff on September 7, 2009 at 3:31 pm

    Is this working for all mobile devices?

  4. Markus Kalliola on September 25, 2009 at 7:56 am

    Hi,

    To help people to do mobile device detection we have published two free ways to do device detection: detectFree and Movila Detection proxy.

    DetectFree works as a service. You need to add a small JavaScript script in to your website and it checks whether the browser is mobile or not.

    Movila Detection proxy is a full HTTP proxy which has mobile device detection as a one feature.

    Link: Mobile device detection

    Hope this helps.

  5. Idel on October 11, 2009 at 3:35 pm

    The “Apache Mobile Filter” is one of the modules of “Apache Module Registry” portal (http://modules.apache.org/search.php?id=1787)

  6. Gaurav on November 21, 2009 at 12:10 am

    Thanks for these modules, i was searching for this only.

  7. olivia on November 23, 2009 at 9:20 pm

    Uhh! This is a dream come true. It solves SOOOO many problems.

  8. redesign soft on May 3, 2010 at 10:32 am

    Thanks for it, it is working very well

  9. Royce Jordon on June 11, 2010 at 7:56 pm

    Hi there may I reference some of the information found in this entry if I reference you with a link back to your site?

  10. Larita Sthole on June 11, 2010 at 11:13 pm

    Fantastic site I have been looking for such information for a long time, glad to have found it and will come again soon. Thanks

  11. trysordy on October 4, 2010 at 6:01 am

    I am glad that I finally understand how to do it… thanks

  12. basiq101 on October 19, 2010 at 1:18 pm

    I am not on wordpress. I have my header as a standalone in ‘header.php’ The difference between ‘mobile.php’ and ‘mobile_device_detect.php’ is the first 65 lines of description have been deleted.

    I am receiving the following error message:

    “Warning: Cannot modify header information – headers already sent by (output started at …/index.php:9) in …/mobile.php on line 100″

    line 9 of index.php =

     

    line 100 of mobile.php is line 165 of mobile_device_detect.php=

    header('Location: '.$redirect); //redirect to the right url for this device

    any ideas? I’m new to everything

    • c.bavota on October 20, 2010 at 11:12 am

      The problem is that you obviously have something being outputted to the page before you try to redirect through the header command. Nothing can be outputted to the page before a header command is executed. You can have functions and such but nothing can be outputted.

  13. basiq101 on October 19, 2010 at 1:20 pm

    line 9 of index.php=

    include ('header.php');
  14. seo contact on November 11, 2010 at 1:43 pm

    I have a question: why to redirect it? what is the relevance?

  15. Bill on December 30, 2010 at 5:10 am

    I want to redirect to antoher page. I use this statement in my if condition.. EVERY TIME condiotin is true but this error occurs. I AM SICK OF THIS

    Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\Unnamed Site 2\work\databasecon\dbcon.php:12) in C:\xampp\htdocs\Unnamed Site 2\work\save_userdata.php on line 23

    • c.bavota on December 30, 2010 at 11:05 am

      That error occurs because something has been sent out before the redirect. Your redirect must be included before anything is displayed on your web page.

  16. wayne on May 19, 2011 at 10:29 pm

    This is not working for me………I am using wordpress – my code is

     

    in my header.php file and I placed the mobile_device_detect.php file in the root directory of the wordpress installation (same directory as the .htaccess file).

    • wayne on May 19, 2011 at 10:31 pm

      this is my code…

      require_once('mobile_device_detect.php');
      mobile_device_detect(true,true,true,true,true,true,true,'http://www.abacusecommercesolutions.mobi',false);
    • c.bavota on May 20, 2011 at 9:56 am

      You got a lot of trues in there. Make sure to follow the tutorial. If you are having issues, you should check out http://detectmobilebrowsers.mobi/

  17. wayne on May 20, 2011 at 4:16 pm

    I did actually. I used their function generator and that was the output it gave me then copied the files to where I said I did….. Have you got any more ideas….?

    thanks

  18. Darren on May 25, 2011 at 8:01 am

    This method works great – I use it for http://www.digisolved.com and it works great!