Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect iPad in php?

Tags:

php

ipad

How do I serve a different page to iPad viewers?

like image 229
Adam Davis Avatar asked Apr 20 '10 16:04

Adam Davis


4 Answers

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
}
like image 170
karim79 Avatar answered Nov 01 '22 03:11

karim79


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.

like image 6
T.J. Crowder Avatar answered Nov 01 '22 03:11

T.J. Crowder


Also, even more simplistic but possibly not as accurate.

if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
   echo "You are on an iPad";
}
like image 4
crmpicco Avatar answered Nov 01 '22 02:11

crmpicco


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".

like image 1
Tom Cabanski Avatar answered Nov 01 '22 04:11

Tom Cabanski