Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

iPad Detection Using JavaScript or PHP

21 Responses »
iPad

The hottest device out there right now seems to be the iPad. iPad this, iPad that, iPod your mom. I'm underwhelmed with the device but that doesn't mean I shouldn't try to account for such devices on the websites I create. In Apple's developer tip sheet they provide the iPad's user agent string:

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10

Given that string we can create a few code snippets to determine if the user is being a smug, iPad-using bastard.

The JavaScript

var isiPad = navigator.userAgent.match(/iPad/i) != null;

A quick String.match regular expression test can check for the presence of "iPad" in the user agent string.

The PHP

$isiPad = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');

This time we look for the position of "iPad" in the user agent string.

The .htaccess

RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://ipad.yourdomain.com [R=301]

Using some logic from Drew Douglass' excellent mobile redirection post, we can redirect users to a mobile version of your website if you so desire.

So what would you the above tests for? You may want to redirect iPad users to a different version of your website. You may want to implement different styles to your standard website if your user is surfing on an iPad.

Discussion

  1. April 7, 2010 @ 9:18 am

    Nice tutorial. Did you get an iPad? I also created a similar tutorial
    http://cardonadesigns.com/wordpress/2010/04/04/using-php-to-detect-the-ipad-user-agent/

  2. April 7, 2010 @ 9:24 am

    The cruddy thing about the iPad user agent string is that it has the word “mobile” in it. So if your detection script is looking for “mobile” already, it may also be sending iPad users to your iphone/mobile optimized site. Apple offered up a way to test though -> http://developer.apple.com/safari/library/technotes/tn2010/tn2262.html

  3. rob
    April 7, 2010 @ 9:37 am

    Great! Now I can redirect/block iPad users!

  4. April 7, 2010 @ 10:22 am

    @Rob: That’s what I was thinking! Thanks David.

  5. April 7, 2010 @ 10:24 am

    @Rob: That’s what I was thinking! Thanks David.

  6. serhey dolgushev
    April 7, 2010 @ 2:49 pm

    for PHP will be better
    $isiPad = strpos($_SERVER['HTTP_USER_AGENT'],’iPad’) !== false;

  7. April 7, 2010 @ 4:53 pm

    I just use this code:

    // <![CDATA[
    if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
    document.location = "WEBSITE GOES HERE";
    }

  8. April 7, 2010 @ 4:54 pm

    @Connor Crosby: that is in a javascript

  9. April 11, 2010 @ 9:31 pm

    Another, and often better option is media queries in CSS3

    http://www.w3.org/TR/css3-mediaqueries/

    You can deliver CSS based on device characteristics, such as viewport width, and orientation (so you can have different CSS for portrait and landscape)

    http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariWebContent/OptimizingforSafarioniPhone/OptimizingforSafarioniPhone.html

  10. seb
    April 12, 2010 @ 4:09 am

    Let them eat Flash

  11. April 12, 2010 @ 4:51 pm

    Welcome to the old web !

    “Site optimised for IE 5.5″

  12. noetic
    April 13, 2010 @ 8:44 am

    But where is the code to make the iPad melt once detected? :p

  13. maybe
    April 13, 2010 @ 9:20 am

    @Noetic: you could start with js “while……..” =x

  14. April 14, 2010 @ 4:00 pm

    Very nice tutorial, tnx man!

    V.

  15. matt
    April 14, 2010 @ 9:50 pm

    I was a little stuck. I’m looking to do something similar, but really just target the viewport for the iPhone/iPod Touch and iPad. Here’s what I came up with so far but doesn’t seem to work. Any suggestions?

    <?
    $useragent = $_SERVER['HTTP_USER_AGENT'];
    if(preg_match("/ipad/", $useragent)) {
    echo '’;
    }
    else if(preg_match(“/iphone/”, $useragent) || preg_match(“/ipod/”, $useragent)){
    echo ”; }
    ?>

  16. charles
    April 21, 2010 @ 10:20 am

    Hey ! Thanks for the turorial !
    I’m a newbie, and I wanna do something I guess is easy, but can’t find a solution.
    What I wanna do is to display a certain element (a div) if the user is on his computer, but display another one instead if he is on his iPad.

    What should I write, and where ????

    Please help me !

    Charles

  17. June 1, 2010 @ 4:29 pm

    This post is featured on 40 iPad tools, tips for designer

  18. jamus
    June 25, 2010 @ 4:15 am

    Perfect! thank you David!

  19. July 18, 2010 @ 8:44 am

    Hi! Thanks for the article.

    I wrote a free open source set of APIs for web designers to detect mobile visitors. The APIs are very light weight, easy to implement, and easily customized. They’re available for JavaScript, PHP, Java, and ASP.NET. Check out http://www.mobileesp.org

  20. jhs
    August 6, 2010 @ 1:05 pm

    Nice article, thanks.

    Also nice site and article on iPad site dev Garmahis. Particularly enjoyed the link to page flipping versus scrolling. :)

  21. luis_green
    August 13, 2010 @ 7:18 pm


    $useragent = $_SERVER['HTTP_USER_AGENT'];
    if(preg_match("/ipad/", $useragent)) {
    die("It Sucks....")
    }else
    {
    .
    .
    .
    .
    .
    }
    ?>

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!