Handset Detection in 2 easy steps.

Published August 20, 2008 by Richard

Note : This article is out of date – Please see the resources section to get up and running in no time at all.

We’ve had a few support emails over the last week or so with folks writing in to say ‘Looks Great, how do i get it working on my site’. So I’ve put together this little “How To” that should help to get you started.

- NOTE : For this How To you’ll need PHP on your web server.

1) Copy the script below into a file called redirect.php

2) Open that up in your fav text editor. We need to make a few changes.

Here’s the bits that need changing.

// Put Your API Key in Here
define(‘APIKEY’,’00000000000000000000000000000000′);

// Put Your NON Mobile site homepage URL in here
define(‘STDSITE’,'http://www.zaption.com’);

// Put your Mobile site URL in here
define(‘MSITE’,'http://m.zaption.com’);

- Put your APIKEY from handsetdetection into top define (copy it over all those 0′s).
- Change the URL in STDSITE to your standard website.
- Change the URL in MSITE to your mobile site.

Bingo – Its all done – now you have a page which can do the detection and
redirect people to the most appropriate site for their device.

—————————————————————————————————

<?php

// HTTP/Request from PEAR.
include "HTTP/Request.php";

// Put Your API Key in Here
define('APIKEY','00000000000000000000000000000000');

// Put Your NON Mobile site homepage URL in here
define('STDSITE','http://www.zaption.com');

// Put your Mobile site URL in here
define('MSITE','http://www.handsetdetection.com');

// This is the handset detection server to query - No change needed here.
define('HD_SERVER','http://c1.handsetdetection.com');

function sendjson($data, $url) {
	$tmp = json_encode($data);
	$req =& new HTTP_Request($url);
	$req->addHeader("Content-Type", "application/json");
	$req->setMethod(HTTP_REQUEST_METHOD_POST);
	$req->addRawPostData($tmp);
	$req->sendRequest();
	$reply = $req->getResponseBody();

	return json_decode($reply, true);
}

function doDetect() {
	$options = array('geoip', 'product_info');

	$data = array();
	$data['apikey'] = APIKEY;

	// Pick up user agent from headers passed to the server
	$data['User-Agent'] = $_SERVER['HTTP_USER_AGENT'];
	$data['ipaddress'] = $_SERVER['REMOTE_ADDR'];

	// Passing $_SERVER options in is optional.
	$data = array_merge ($data, $_SERVER);

	$result = sendjson($data, HD_SERVER."/devices/detect.json");

	return $result;
}

$ret = doDetect();
if ($ret['message'] == 'OK') {
	header('Location: '.MSITE);
} else {
	header('Location: '.STDSITE);
}

?>


Posted In How To | 9 Comments

9 Comments

jaime
Nov 18, 2008

hi,

it didnt worked for me.
infact i would need to found if the device is wap2 enabled or not.

case 1: is not mobile = my www domain
case 2: support wap2 = my wap2 domain
case 3: dont support wap2 = my wap domain

kind regards,

admin
Nov 18, 2008

Hi Jaime,

Thanks for trying our service.

When you say ‘It didnt work’ – Do you mean that you had problems using the API ?

Cheers
r

BleacherBoy
Dec 3, 2008

Do we need to put any additional script in our index (or any other) page, or just install this .php into our root folder?

Thanks!

admin
Dec 9, 2008

@BleacherBoy

You’ll need PHP Pear installed and the Pear HTTP/Request package. There’s another script in the forums which will run without pear just in case you dont have pear installed.

Cheers
Richard

marlo
Jan 16, 2009

ummm….i dont get this stuff…can you elaborate more about this thing??umm..about the API KEY?? whats that??where can i get that?

admin
Jan 16, 2009

@marlo – Once you sign up then you can get an APIKey. You need that to access our API so that your website can detect iPhone, detect Blackberry, detect Nokia etc.. devices.

Get the full scoop in the FAQ – http://www.handsetdetection.com/pages/faq

Cheers
r

Matt
Feb 2, 2009

I have a website that is written in HTML, pretty old-school, with tables and what-not. (www.coreessentials.net) Will copying this code with the edits and putting it in the body of the my html code make it work? Also, I have been poking around and trying to figure out what my API is, but no luck yet…

Thanks

seeker
Mar 14, 2009

i am desperately trying to make my flash website detect and redirect mobile browsers and ive been reading this over and over and cant figure this thing out. 1. unable to obtain API 2. unable to get info on how to add script to index page of a flash based website. 3. would actionscript be of any benefit, if so how would i

please can someone help me i been trying off and on for 2 months.

David
May 6, 2009

Great idea, but sadly your documentation is very vague on how we use the product. You have left steps out assuming we are all server savvy folks.

How about a simple, easy to understand step by step?

1. Create a php file with X code placed inside
2. Upload X php file to server and name this file X

See where I am going here.

Pointing to the FAQ isn’t doing anyone any good that isn’t understanding the system.

Hope this site will react and get the support and how to’s working, again a great idea.

Post a Comment

If you’d like a picture to show up by your name, Get a Gravatar