How to detect iPhone, iPod and iPad with PHP

Use the User Agent to detect if the vistor is using an Apple device and wich one (iPhone, iPod or iPad)

I've tested this snipped with Safari wich allows you to change the User Agent.

// Apple detection
$Apple = array();
$Apple['UA'] = $_SERVER['HTTP_USER_AGENT'];
$Apple['Device'] = false;
$Apple['Types'] = array('iPhone', 'iPod', 'iPad');
foreach ($Apple['Types'] as $d => $t) {
$Apple[$t] = (strpos($Apple['UA'], $t) !== false);
$Apple['Device'] |= $Apple[$t];
}
// Apple device?
echo
"

Apple device? ", ($Apple['Device'] ? 'true' : 'false'),
"

\n

iPhone? ", ($Apple['iPhone'] ? 'true' : 'false'),
"

\n

iPod? ", ($Apple['iPod'] ? 'true' : 'false'),
"

\n

iPad? ", ($Apple['iPad'] ? 'true' : 'false'),
'

';

About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*