[Phpwm] mobile redirect
Simon Emms
simon at simonemms.com
Mon Nov 1 11:56:43 UTC 2010
Howdy
Possibly a bit late (I’ve been on my holidays), but I wrote one a couple of weeks ago. It uses PHP’s getbrowser() function (but as an object, rather than the native one which is poorly supported) which, happily, one of the object’s elements is isMobileDevice which I think you can guess what it does.
For details on how to use the Browscap object file, see http://php.net/manual/en/function.get-browser.php. There’s a lot of things you can do with it. You instantiate it like this:
$objBrowscap = new Browscap(/location/to/writable/cache/dir);
$objBrowser = $objBrowscap->getBrowser();
You need to specify a cache directory as it downloads the data for all browsers which is about 400kb. The first time takes about 2 seconds to download, but is instant once it’s cached.
More info (and the original class file) can be found at http://garetjax.info/projects/browscap/
The Mobile object file is my implementation of the class for my framework. The key stuff is in line 54:
If(isset($objBrowser->isMobileDevice) && $objBrowser->isMobileDevice == ‘true’) {
/* This is a mobile browser – do what you have to do */
}
I should point out that I’ve done an isset on the isMobileDevice. Every browser I’ve tested it with (IE6 – 8, FF, Chrome, Safari, Opera, iPhone and various other mobiles) declare this, but sod’s law dictates there will be on that doesn’t.
Also, it returns a string of either true or false, not a Boolean. This is a bit of a pain in the arse, but not the end of the world I guess.
Hope that helps you guys.
S
From: phpwm-bounces at mailman.lug.org.uk [mailto:phpwm-bounces at mailman.lug.org.uk] On Behalf Of Mo Awkati
Sent: 27 October 2010 20:22
To: West Midlands PHP User Group
Subject: Re: [Phpwm] mobile redirect
Many thanks Carl, much appreciated
Mo
_____
From: Carl Sutton <dogmatic69 at gmail.com>
To: West Midlands PHP User Group <phpwm at mailman.lug.org.uk>
Sent: Wed, 27 October, 2010 20:17:58
Subject: Re: [Phpwm] mobile redirect
mo
when you use cake with that its quite nice as you can just use different html layouts and views, leaving all the core php and models as is. makes maintenance much easier.
Carl Sutton
dogmatic69.com
On Wed, Oct 27, 2010 at 8:14 PM, Mo Awkati <mawkati at yahoo.co.uk> wrote:
_____
From: Carl Sutton <dogmatic69 at gmail.com>
To: West Midlands PHP User Group <phpwm at mailman.lug.org.uk>
Sent: Wed, 27 October, 2010 19:42:21
Subject: Re: [Phpwm] mobile redirect
for serious mobile stuff there is this http://wurfl.sourceforge.net/
if you want something more simple have a look at this http://github.com/cakephp/cakephp/blob/master/cake/libs/controller/components/request_handler.php#L356
redirecting is simple http://php.about.com/od/learnphp/ht/phpredirection.htm
Carl Sutton
dogmatic69.com
On Wed, Oct 27, 2010 at 7:05 PM, Mo Awkati <mawkati at yahoo.co.uk> wrote:
Hi,
Can anyone suggest a decent script for detecting mobile phone browsers and redirecting
visitor to the mobile phone version of the site?
Many thanks
Mo
Cheers Carl, the Cake PHP is more what I was looking for. I did google around but did not not come across this one.
many thanks
Mo
_______________________________________________
Phpwm mailing list
Website : http://www.phpwm.org
Twitter : http://www.twitter.com/phpwm
Facebook: http://www.facebook.com/group.php?gid=2361609907
Post to list: Phpwm at mailman.lug.org.uk
Archive etc : https://mailman.lug.org.uk/mailman/listinfo/phpwm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/phpwm/attachments/20101101/963bffb1/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Browscap.class.php
Type: application/octet-stream
Size: 18343 bytes
Desc: not available
URL: <http://mailman.lug.org.uk/pipermail/phpwm/attachments/20101101/963bffb1/attachment-0002.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Mobile.class.php
Type: application/octet-stream
Size: 1514 bytes
Desc: not available
URL: <http://mailman.lug.org.uk/pipermail/phpwm/attachments/20101101/963bffb1/attachment-0003.obj>
More information about the Phpwm
mailing list