Home > Mobile >

Free PHP script for detecting devices! (2)


10-14-2014 10:15 AM #1 tomsko (Member)
Free PHP script for detecting devices!

Sup guys, so I just wrote this PHP script for my own needs and decided to share with you. I didn't want to use handsetdetect as they charge after a few k detections.
Basically what it does is it takes both DEVICE & BRAND token from your Voluum url, IF you pass it in the landing page, but if it fails to pass the device/brand from Voluum, it goes other way and detects user agents itself, and basically spits out just the OS name (or also device in Appl case).

So here is example of your Voluum landing page link that you should set up:
http:/yourlink.com/somedir/otherdir/?brand={brand}&model={model}
This link will pass for example ?brand=Apple&model=iPhone. Now with PHP we extract this.

My code:

Code:
<?php
if (isset($_GET['brand']) || isset($_GET['model'])) {
    $brand = $_GET['brand'];
	$model= $_GET['model'];
	$dev = $brand." ".$model;
}
else 
{
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
$ipad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
$windowsphone = strpos($_SERVER['HTTP_USER_AGENT'],"IEMobile");
if ($iphone) {
$dev = "iPhone";
}
else if ($android) {
$dev = "Android";
}
else if ($palmpre) {
$dev = "Palm";
}
else if ($berry) {
$dev = "BlackBerry";
}
else if ($ipod) {
$dev = "iPod Touch";
}
else if ($ipad) {
$dev = "iPad";
}
else if ($windowsphone) {
$dev = "Windows Phone";
}
else if (!$android || !$palmpre || !$berry || !$ipod || !$ipad || !$windowsphone) {
$dev = "Smartphone";
}
}
?>
How to print device name and brand:
Simply put <?php echo $dev; ?> wherever you want this to be printed out.

Code explained in human language:
1) Check if ?brand and/or ?model is passed by link
2) If either of 2 tokes is passed, set $dev variable to "Brand + Model"
3) If brand & modelis not passed by URL, go detect visitors user agent
3) If user agent is Android, $dev = Android, if it's iPhone, $dev = iPhone and so on.
4) If Also user agent detect fails, $dev is simply "Smarthphone"

P.S. Don't forget that the file you do this on must be .php, not .html. So go and rename your index.html to index.php.
Using PHP will put additional load on your server, but user-end loading will be faster as there are no scripts his device must execute.
I aint no PHP programmer but I do understand the basics. My code probably could be shortened and/or improved so feel free to do so if you know this language.

Enjoy!


10-15-2014 09:51 AM #2 bbrock32 (Administrator)

Very handy script mate, thanks for sharing!


Home > Mobile >