How do I serve a different page to iPad viewers?
+4
A:
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 http://developer.apple.com/safari/library/technotes/tn2010/tn2262.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
}
karim79
2010-04-20 16:35:23
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
2010-04-20 16:37:15
-1 for Tom's reasoning. This will almost certainly break when the OS upgrade for 4.0 rolls out.
Stefan Kendall
2010-04-20 16:38:04
@Tom - was editing that in right before your comment
karim79
2010-04-20 16:38:06
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
2010-04-20 16:44:49
@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
2010-04-20 16:49:29
+5
A:
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
2010-04-20 16:35:40
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.
David Lively
2010-04-20 16:49:15
+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
2010-04-20 16:49:42
@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
2010-04-20 23:54:05
+1
A:
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
2010-04-20 16:35:54