track ip addresses, phone numbers, etc

Detect iPad in PHP, Perl, JavaScript, Nginx, Apache, Lighttpd

Published on 10 April 2010

This article shows how to programmatically detect an iPad visitor from the user-agent in PHP, Perl or JavaScript and configure it at webserver level for Nginx, Apache and Lighttpd servers.

The iPad's user-agent string for Safari is (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

To forward all iPad visitors from http://aruljohn.com to http://ipad.aruljohn.com, this is what to do.

Using PHP

<?php
if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
  location("http://ipad.aruljohn.com/");
  exit;
}
?>

Using Perl

Make sure this is the only print statement. Do not allow any other header to be output before this.

#!/usr/local/bin/perl
if (index($ENV{'HTTP_USER_AGENT'}, 'iPad')) {
    print "Location: http://ipad.aruljohn.com\n\n";
    exit;
}

Using JavaScript

Use the navigator object in JavaScript.

if (navigator.userAgent.match(/iPad/i)) {
    location.href='http://ipad.aruljohn.com';
}

Using Nginx webserver

Edit nginx.conf. Use the variable $http_user_agent to match the user-agent string containing iPad.

server {
    listen      80;
    server_name aruljohn.com;
    location / {
        if ($http_user_agent ~ iPad) {
            rewrite     ^(.*) http://ipad.aruljohn.com$1 permanent;
        }
    }
}

Using Apache webserver

Edit httpd.conf, apache.conf or .htaccess and add this:

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

Using Lighttpd webserver

Edit lighttpd.conf and add this:

$HTTP["useragent"] =~ "iPad" {
    url.redirect = ( "^/(.*)" => "http://ipad.aruljohn.com/$1" )
}

Created on 10 April 2010


Affiliate Disclosure: Some of the links to products on this blog are affiliate links. It simply means, at no additional cost to you, we’ll earn a commission if you click through and buy any product.