Home > Mobile >

Simple Mobile/Desktop splitter script (5)


07-04-2012 10:00 AM #1 tijn (Moderator)
Simple Mobile/Desktop splitter script

Here is a php script that you can use to split your traffic between mobile and non-mobile.

You can use it to send mobile traffic to a mobile optimized offer for example.

As a favour to a friend I dug this out of my archive of scripts and updated it with some of the newer mobiles.

The script filters for keywords in the User Agent string.

The filters are based on the list of mobile User Agents

Here is the script:

[php]
<?
//mobile redir script

//url settings
$url1 = 'http://google.com/search?q=im+a+mobile';
$url2 = 'http://google.com/search?q=im+not+a+mobile';

//filters
$filter = "/iPhone|iPad|Android|Windows CE|240x320|320x320|PDA|BlackBerry|BlackBerry|Phone OS|Windows Phone|Mobile Safari|Smartphone|Nokia|Opera Mini|SonyEricsson|Danger hiptop|DoCoMo|Vodafone|Phone|Minimo|Mobile|Tablet| Nexus|T-Mobile|Sprint|Orange|IEMobile|HTC|PalmOS|AvantGo|A vantGo|PalmSource|Netfront|SAMSUNG/i";

//get user agent
$ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$ua = isset($_GET['ua']) ? $_GET['ua'] : $ua;
$ua = empty($ua) ? '#NA' : $ua;

//check
$r = preg_match($filter, $ua, $m);

if ( !$r ) {
//its not a mobile, so send to location 2
header("Location:$url2");
} else {
//matched one of the mobile filters - send to location 1
header("Location:$url1");
}

?>




[/php]

To set it up just change the URLs for $url1 and $url2 on lines 5 and 6.

You can add a filter by updating line 9 ($filter variable). For example, if a new mobile comes out with the UA string "Fancy Mobile Brick (blah; blah; blah", then you just add the filter text to the front of the string like so:

[php]
$filter = "/Fancy Mobile Brick|iPhone|iPad|Android|Windows CE|240x320|320x320|PDA|BlackBerry|BlackBerry|Phone OS|Windows Phone|Mobile Safari|Smartphone|Nokia|Opera Mini|SonyEricsson|Danger hiptop|DoCoMo|Vodafone|Phone|Minimo|Mobile|Tablet| Nexus|T-Mobile|Sprint|Orange|IEMobile|HTC|PalmOS|AvantGo|A vantGo|PalmSource|Netfront|SAMSUNG/i";




[/php]

Keywords in the filter need to be separated by | symbol, and wrapped in "/.../i"


07-04-2012 11:08 PM #2 deondup (Member)

This could be integrated into Prosper and would make a great add-on...having the ability to direct traffic based on useragent


07-05-2012 07:54 PM #3 mrgodlike (Member)

Is there a way to host this say on my cdn and then just use like URL variables in each of your landers based off of the script?

Hhope that makes sense... i'm severely coding illiterate


07-05-2012 09:55 PM #4 tijn (Moderator)

CDNs dont do php.

A javascript version of this code could be hosted on a cdn.

Something like this will probably work:

HTML Code:
<html>
<head>
<title>Your lander</title>
<script>
var ua = navigator["userAgent"];
if ( ua.match(/iPhone|iPad|Android|Windows CE|240x320|320x320|PDA|BlackBerry|BlackBerry|Phone OS|Windows Phone|Mobile Safari|Smartphone|Nokia|Opera Mini|SonyEricsson|Danger hiptop|DoCoMo|Vodafone|Phone|Minimo|Mobile|Tablet|Nexus|T-Mobile|Sprint|Orange|IEMobile|HTC|PalmOS|AvantGo|AvantGo|PalmSource|Netfront|SAMSUNG/i) !== null ) {
	//match - redirect	
	window.location.href="http://google.com/search?q=i+am+a+mobile";
}
</script>
</head>
<body>
This is your normal lander
</body>
</html>


03-21-2013 12:36 PM #5 julien (Member)

Tijn, your script has resulted in a 10% increase in my leads, for a campaign supposed to be targeted to Web.

This is just awesome!

Thank you a million time.


Home > Mobile >