SHARE
Facebook X Pinterest WhatsApp

How Can I Detect the iPhone & iPad’s User Agent?

Written By
thumbnail
Scott Clark
Scott Clark
Jun 17, 2010

So you’ve decided to create a mobile website for iPhone and iPad users, rather than creating a web applications. Where do you start? This tutorial will show you how to detect the popular mobile devices using JavaScript, PHP and your site’s .htaccess file!

Detecting the iPhone and iPad

Obviously, if you’ve created a page specifically for iPhone and iPad users, the first thing you’ll need to do is to find out when a user is visiting your site via an iPhone or iPad. Apple provides the iPad’s user agent string, which appears as follows:

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

The iPhone’s user agent string appears as follows:

HTTP_USER_AGENT=Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C25 Safari/419.3

Provided with this information, it’s fairly straight forward to use JavaScript to detect the user agent, and send the user to a specific page:

<script type=”text/javascript”>
if((navigator.userAgent.match(/iPhone/i)) ||
 (navigator.userAgent.match(/iPod/i))) {
   if (document.cookie.indexOf(“iphone_redirect=false”) == -1) window.location = “http://iphone.yoursite.com/”;
}
</script>

Using the same information, you can do the dirty work on the server side using PHP instead:

<?php
if(strstr($_SERVER[’HTTP_USER_AGENT’],’iPhone’) || strstr($_SERVER[’HTTP_USER_AGENT’],’iPod’))
 {
  header(‘Location: http://yoursite.com/iphone’);
  exit();
}
?>

Finally, you could always use your site’s .htaccess file to do the same thing on the server side of things:

RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://ipad.yourdomain.com [R=301]

Now, assuming you want to purchase a ready-made package that detects all mobile devices, you can visit the Detect Mobile Browsers website for their $50 PHP-based mobile device detection script. It is free for non-profit or personal use, however commercial websites need to purchase it. Not only will it detect iPhones and iPads, it will also detect Android, Opera Mini, Blackberry, Palm and Windows devices. You can even use their function generator to create the specific action you wish to, once the user agent is detected. Actions include treating the user agent as a mobile device, directing the user to a specific page for the device, or displaying the page normally as it would for any other browser.


Next week we’ll get into the creation of iPhone and iPad specific websites, show you how to detect and set the iPhone and iPad’s viewport orientation using JavaScript and more!

Recommended for you...

How to Make Your Site Voice Search Ready
Working with the CSS Image Gallery
HTML Geolocation: How to Find Someone’s Location
Octavia Anghel
Dec 10, 2018
Ten Basic Things Every Web Developer Should Know
Bradley Jones
Dec 3, 2018
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.