Skip navigation
Currently Being Moderated

Need code for mobile browser detection

Nov 20, 2012 1:41 PM

Tags: #jquery #dreamweaver #mobile #browser_detection

So I've read there are several different ways to do this using HTML, JS, PHP, Cookies, etc... and found a discussion suggesting to search for "javascript detect mobile browser" which I did, and maybe since the forum is from 2010 the search results are lacking is a link to that forum:


I have code and editing knowlege but not on the developer side of things...I could not write a JS, or PHP if needed...but have created a jQuery mobile site no problem!




  • Currently Being Moderated
    Nov 20, 2012 2:44 PM   in reply to TribJenny

    First tell us why do you think you need browser detection?



    Nancy O.

    Mark as:
  • Currently Being Moderated
    Nov 21, 2012 10:09 AM   in reply to TribJenny

    If your site is hosted on a Linux server, you could use .htaccess or PHP redirect.  Client side script (js) is not as reliable because the end user must have JavaScript enabled or it won't work.



    Nancy O.

    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Nov 21, 2012 7:19 PM   in reply to TribJenny



    Like Nancy said, JS is not a reliable option.


    DetectMobileBrowser is something I've tried in the past and has not worked with most devices the way it should.



    Mark as:
  • Currently Being Moderated
    Nov 27, 2012 11:04 AM   in reply to TribJenny

    If you can utilize .htaccess, the following may be helpful, especially in cases where you might want to link to the main site from the mobile version. I've used a variation of this code on two mobile projects: d-set-a-cookie-to-break-redirect/

    Mark as:
  • Currently Being Moderated
    Nov 27, 2012 11:33 AM   in reply to TribJenny

    You could use either JavaScript or PHP to do this but I prefer PHP, as Nancy said JavaScript can be a bit shakey to say the least across different devices and it may not be enabled.


    I have come across a few ways to this but my preferred choice is a project called MobileESP. This project offers a JavaScript or a PHP solution for detecting a massive array of different devices. Because of how complex this project is, I thought I'd explain a very basic way of using the scripts to redirect the client to the appropriate page dependant on what type of device the client is using. PHP is the best solution for this. This script was acquired from there site.


    Go to and download the PHP version of MobileESP which is a file called mdetect.php.


    Put this file in a folder called scripts.


    Then for your index.php home page at the site root, add this script below,




    //Instantiate the object to do our testing with.

    $uagent_obj = new uagent_info();


    //Detect iPhone Tier and iPads...

    if (($uagent_obj->DetectTierIphone() == $uagent_obj->true) ||

    ($uagent_obj->DetectIpad() == $uagent_obj->true))

      { header('Location:'); } //Edit here


    //Detect Rich CSS Tier...

    else if ($uagent_obj->DetectTierRichCss() == $uagent_obj->true)

      { header('Location:'); } //Edit here


    //Detect All Other Mobile Devices...

    else if ($uagent_obj->DetectTierOtherPhones() == $uagent_obj->true)

      { header('Location:'); } //Edit here


    //Else it's a regular PC browser -- send to regular desktop site


      { header('Location:'); } //Edit here




    This script redirects iPhone Tier devices and iPads to, Rich CSS devices such as Nokia N95 to, Generic mobile devices to and Desktop browsers and all others to


    You can also redirect to any file i.e. or


    Hopefully this should answer your question.


    Mark as:
  • Currently Being Moderated
    Nov 27, 2012 12:11 PM   in reply to Chillax86

    I would mention that Project 7 has a mobile detection script that runs correctly, written in JavaScript. And then what they do is they change how the website looks in CSS. That way they don't have to support mobile in a separate website.


    This technology is in Page Builder Magic and in their Adaptations websites. For very little cost, their templates can give you mobile capabilities without any of the headaches, assuming time is money.

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points