Posted by James Brooks on 11th April 2010

Snippets: iPad Detection

It was only the other day that Apple started to sell their new iPad product. Personally I’m not impressed with the product at all and see it as the oversized iPod, yet undersized anything else. I can however, see what people mean when they say that it will transform the way we receive media; we’ll receive it on something that will limit everything else we can do. $499 for a collection of media, and the rest for the iBooks (is that right?).

Anyway, the other day I was studying website traffic, seeing what my new blog generates et cetera, when I found a rather peculiar user 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

Confused, I made a quick search to confirm the iPads user-agent string and promptly found the Apple Developer notes. Turns out I had an iPad user visit my website. Fighting my horrendous self control, I decided not to make a redirect telling them to get off my website, but I thought I’d share the code to do so, if others wished to, if others wished to show another version of their website to those awesome iPad users.

PHP

PHP has the ability to check the user-agent using the $_SERVER predefined variable. Similar to the method we’ll use in JavaScript, we can check that the word “iPad” is in the string.

When checking my code, I found that David Walsh had already provided a much better way to check if the iPad is in the string or not, so instead I’ll show you his code – This code is written by David Walsh.

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

It’s neat and I like the fact that $isiPad becomes a boolean thanks to the typecast.

JavaScript

This will make my Client Side lecturer happy. He’s a big believer in user agents, using indexOf and JavaScript… Here is my version of the code, a little messy, but it serves its job well.

var user_agent = navigator.userAgent.toLowerCase();
var is_iPad = ((agent.indexOf('iphone') != -1);

Or again, David Walsh provided a JavaScript method too, cleaner but uses regular expressions to make a better match.

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

If you’re familiar with .htaccess then you can use the brains of Brian Cray to redirect the user based on {HTTP_USER_AGENT} check it here.

Share and Enjoy:
  • Print
  • Twitter
  • Digg
  • StumbleUpon
  • Google Bookmarks
  • del.icio.us
  • Facebook
  • Live
  • LinkedIn

    2 Responses

  1. Fairy Kerne says:

    My friend told me about your blog, so I thought I’d come have a read. Very interesting reading, will be back for more!

  2. I finally decided to drop a comment, and let me tell you this is another very strong and powerful post. I’ve been reading through some of your previous posts and have been visiting your blog every now and then. Take a look at Google Caffeine ebook and let me know what you think, it definitely helps you rank sooo much higher in search results…

    Anyways, good luck on your blog, and feel free to check out : Google Caffeine

    Thanks so much!

    All the best,
    Dino Vedo

Post your comments