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 "<p>Apple device? ", ($Apple['Device'] ? 'true' : 'false'), "</p>\n<p>iPhone? ", ($Apple['iPhone'] ? 'true' : 'false'), "</p>\n<p>iPod? ", ($Apple['iPod'] ? 'true' : 'false'), "</p>\n<p>iPad? ", ($Apple['iPad'] ? 'true' : 'false'), '</p>';
Code language: PHP (php)

About Ricard Torres

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.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

Leave a Reply

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