5

How do I serve a different page to iPad viewers?

Adam Davis
  • 91,931
  • 60
  • 264
  • 330
  • Careful, seems that you cannot use the user agent anymore! See https://stackoverflow.com/q/62323230/1066234 (Apple sucks). – Avatar Oct 20 '22 at 13:10

4 Answers4

9
if($_SERVER['HTTP_USER_AGENT'] == 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10') {
    echo "That is an iPad";
}

See https://developer.apple.com/library/content/technotes/tn2010/tn2262/_index.html

Also, if you're not bothered with an exact match, you might contemplate something like:

if(stristr($_SERVER['HTTP_USER_AGENT'], 'Mozilla/5.0(iPad;')) {
    // probably an iPad
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
karim79
  • 339,989
  • 67
  • 413
  • 406
  • 1
    I don't know that I would look for an exact match. I would just look for it to contain "iPad" unless you have a reason to be version specific. – Tom Cabanski Apr 20 '10 at 16:37
  • 1
    -1 for Tom's reasoning. This will almost certainly break when the OS upgrade for 4.0 rolls out. – Stefan Kendall Apr 20 '10 at 16:38
  • This still probably won't work when other devices begin trying to pull the ipad's useragent. You need to be general, but not so general that you pick up garbage. For more information, read the history of the useragent string. This solution is still broken. – Stefan Kendall Apr 20 '10 at 16:44
  • @Stefan - tweaked the second argument to `stristr` to something that's more like to exclude garbage. An alternative would be to use `in_array` and match based on every possible user_agent the iPad could be sending (depending on OS version, etc) – karim79 Apr 20 '10 at 16:49
  • here is a complete solution: http://sjevsejev.blogspot.com/2012/05/php-mobile-detect-class-ipad-iphone.html – Sergej Brazdeikis May 20 '12 at 07:40
  • Be aware that the iPad doesn't give this kind of user-agent string anymore these days. You just get plain WebKit information. So it's likely that you can't detect it anymore in this fashion. –  Dec 31 '13 at 12:24
6

You can sniff the iPad's user-agent header via $_SERVER['HTTP_USER_AGENT'], but ideally, if you can feature-detect the things you want to be different on the iPad vs. any other device, that's more robust and flexible than agent sniffing.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Good answer, but for an OP with <20 posts and this level of question, a bit more detail on what "feature-detect" means might be nice. – 3Dave Apr 20 '10 at 16:49
  • 1
    +1 for feature detection. I've seen several SO questions dealing with explicit touch capabilities and their support in JS - at least on modern browsers. That may be a way to go. @David has a point, too. – Pekka Apr 20 '10 at 16:49
  • @David - I'll probably post another question on feature detection later. I posted this with a new account as I was trying out an iPad and didn't want to use my login. I surprised this isn't a dupe, actually... Since it stuck around and got good answers, I had it merged so I could select an answer. I chose karim79's answer, though, as I prefer answers with code snippets. Thanks T.J.! – Adam Davis Apr 20 '10 at 23:54
4

Also, even more simplistic but possibly not as accurate.

if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
   echo "You are on an iPad";
}
crmpicco
  • 16,605
  • 26
  • 134
  • 210
1

The user agent header in the request will be:

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/7B367 Safari/531.21.10

Notice that it contains "iPad".

Tom Cabanski
  • 7,828
  • 2
  • 22
  • 25