Skip to content Skip to navigation

Detect iPhone via client-side or server-side

Tags: 

http://www.iphonewebdev.com/examples/iphoned.html

You are NOT on an iPhone.

To detect iPhone users on your sites, you can do so client-side or server-side.
Client-side (javascript):

if (navigator.userAgent.indexOf('iPhone') != -1) {
/* iPhone user */
}

Server-side (example is PHP):

if (stristr($_SERVER['HTTP_USER_AGENT'],'iPhone')) {
/* iPhone user */
}

For sites that use an MVC coding pattern where the programming logic code is separate from the template, the following example is useful:
if (stristr($_SERVER['HTTP_USER_AGENT'],'iPhone')) {
$template = 'home/iphone.html';
}
else {
$template = 'home/index.html';
}

Thanks to Jose D. Lopez for the PHP tips.