PDA

View Full Version : Auto Redirect for Handheld Users


patricktt
24-08-2009, 10:14 AM
Hello all, just thought I would post this as I have found it useful. I have updated a file that I found on this website (http://studiohyperset.wordpress.com/2006/12/09/final-update-detecting-and-automatically-redirecting-website-visitors-who-visit-a-standard-webpage-website-on-mobile-handheld-wireless-pda-or-cel-cell-phone-devices-browsers-to-a-mobile-ver/) which redirects a user to a specified url if it detects they are using a handheld device.

All I have done is tidy it up a bit so instead of a million else if statements, it just stores the user agents in an array and then performs one check at the end.

Admittedly this isn't going to be 100% effective, but I think it's a pretty solid effort so thanks to Quimby Melton.

patricktt
24-08-2009, 11:07 AM
saying that though, it does seem like unless you have every version of the browsers and phones listed, it isn't going to stay relevant for long... anyone know how to autodetect a better way?

::Scott::
27-08-2009, 02:13 PM
Hmm that looks yuck. The way I have done this in the past is using PHPs get_browser() function in combination with an up to date browscap.ini file.

<?php
$arrUA = get_browser($strAgent, true);

if($arrUA['ismobiledevice'])
{
header('Location: blah blah');
}
?>

Then keep your browscap.ini up to date all the time with a daily cron:
<?php



define('BROWSCAP_INI', '/usr/local/lib/php_browscap.ini');
define('UPDATE_SITE', 'http://browsers.garykeith.com/versions/version-date.asp');
define('DOWNLOAD_SITE', 'http://browsers.garykeith.com/stream.asp?PHP_BrowsCapINI');

$strTime = file_get_contents(UPDATE_SITE);

if (!file_exists(BROWSCAP_INI) || strtotime($strTime) > filemtime(BROWSCAP_INI))
{
passthru('wget -O "' . BROWSCAP_INI . '" ' . DOWNLOAD_SITE);
passthru('service httpd restart');
} else
{
echo ('browsecap.ini is already up to date' . "\r\n");
}

?>

patricktt
27-08-2009, 04:46 PM
i didn't know there was a get_browser function :) learn something new every day! thanks for the post, I will try it out as it looks a much better solution!

$arrUA['ismobiledevice']

is that a built in key? looking at the php.net site, it doesn't mention this?

Toonidy
27-08-2009, 09:39 PM
is that a built in key? looking at the php.net site, it doesn't mention this?

Looking at the PHP manual on this function, http://au2.php.net/manual/en/function.get-browser.php

There is a note saying:

Note: In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.
browscap.ini is not bundled with PHP, but you may find an up-to-date » php_browscap.ini file here.
While browscap.ini contains information on many browsers, it relies on user updates to keep the database current. The format of the file is fairly self-explanatory.


So I'm guessing this file will put the ''ismobiledevice' in the output (sorry if it is so obvious, just interested at this as well). So I guess it is not built in and requires a configuration setting to point to that file. Just to point that out.

Having said that... I think the only way to detect the handheld devices is to find some kind of database containing up to date list of handheld devices (as already mentioned). But maybe I'm wrong anyway, since I never tried this kind of stuff. I thought I should join in the discussion and learn D:

- Toonidy

Franki
28-08-2009, 09:43 AM
why don't you impliment a handheld CSS with the main site and save yourself a bunch of work with "mobile optimised version"?

example: http://www.tn3.com.au/ (throw whatever device/browsers you have at it)

::Scott::
28-08-2009, 10:08 AM
If you have trouble with get_browser() there is also a few implementations solely in php (if your stuck on a shared host).

Heres one:
http://alexandre.alapetite.fr/doc-alex/php-local-browscap/php-local-browscap.php.txt

I saw another one at one point a while back that had a lot of caching functionality built into it. Cant seem to find it now :(

Franki's solution is good too. Sometimes you might want to do something different for mobiles though - like show a different form. Also your RegExp will probably be outdated in a few years when the mobile useragents change.

If your going to use javascript like that too, you may be better off testing window.screen.width (and maybe window.screen.height).

Franki
28-08-2009, 11:11 AM
If your going to use javascript like that too, you may be better off testing window.screen.width (and maybe window.screen.height).

I hear ya, but I hated the fact I have to resort to JS for detection in the first place because the like of PIE and iPhone ignores the "handheld" directive :s

With my case there's was no point in checking for screen size as the method used catered for +80% of mobile browsing (though about 95% of meaningful users anyway).

Franki
28-08-2009, 11:13 AM
and re useragent change, not much/no difference to the PHP version, ie just add them to the UA list.

::Scott::
28-08-2009, 06:34 PM
Yeah but for a clients site that you dont want to come back to... Useing get_browser() + an update cron on browscap.ini, your future proofing it without any future intervention.

patricktt
01-09-2009, 08:16 AM
why don't you impliment a handheld CSS with the main site and save yourself a bunch of work with "mobile optimised version"?


I have used this method a couple of times before, in this instance though, the mobile functionality is only for one user group, and for that group to perform one function only, where the form is very different to the main site, so I have a couple of pages that are for handheld only, hence why i need to know if they are using handheld to look at it.

Thanks for the responses on this though everyone, it is a learning experience for me too, to see the different options and weigh up the pros and cons.

moosecat
07-10-2009, 11:25 AM
I have found that with just using handheld CSS you don't always catch all mobile devices, in fact many like to be seen as standard browsers. Admittedly they do render well so worth giving the visitor the option - but still an optimise version for mobile media is surely better?!?

The php option that Scott posted will also give you a few options with altering the IA to suite mobile devices. It isn't just the presentation that you may need to change.... it is worth thinking about the content and site structure too!

Franki
07-10-2009, 12:40 PM
just a quick headsup if anyone missed it with the browsercap.ini implimentation - this file is over 300kB in size, so may be an issue for some.

Basil
22-10-2009, 02:57 PM
franki - why is this an issue?

the client doesn't download the ini file, it is just used as a reference by PHP on server side.

PaulR
22-10-2009, 03:07 PM
franki - why is this an issue?

the client doesn't download the ini file, it is just used as a reference by PHP on server side.

get_browser() isn't generally recommended on busy sites as it can bring your server to its knees. See all the comments at http://php.net/manual/en/function.get-browser.php

Franki
22-10-2009, 03:24 PM
franki - why is this an issue?

Per Paul's answer above.

Even if your app have identified the user (and say cookie'd them for their device), unique/new users will still invoke the call. On busy sites, this will be a big issue.

Basil
23-10-2009, 11:02 AM
i would still use it...

just check for the top 10 or so browsers in your code then refer to browscap if it isn't found.