Detect mobile devices with the WURFL API
This component uses the WURFL API to detect any mobile or wireless device, and sets custom paths for your views and css, so you can easily create a mobile version of your website.
This component uses the WURFL API to detect any mobile or wireless device, and sets custom paths for your views and css, so you can easily create a mobile version of your website.
Lets get started.
First of all, download the WURFL API package here (Make sure to download the 1.1 version).
Extract the entire WURFL folder in the package to your /app/vendors folder.
Create a directory /app/configs/wurfl, and extract everything in the resources folder to that directory.
In the examples/resources directory, extract the file in the wurfl-regression.zip to your /app/configs/wurfl directory.
Edit the wurfl-config.xml file to look like the following:
Download code
Create the folder /app/tmp/cache/wurfl.
Create a file /app/controllers/components/mobiledetect.php and put the following in the file:
In your app_controller, remember to include the component like this:
Download code
and add to the beforeFilter function the following:
Download code
And thats it! Now you can create a custom layout file for your mobile website, located under /app/views/layouts/mobile.
You also need to put your css in a directory in /app/webroot/themed/mobile/css
and (optional)images in /app/webroot/themed/mobile/css/img.
Your views need to go to /app/views/themed/mobile.
Your done! Now you can create a mobile version of your website.
Lets get started.
The WURFL API
First of all, download the WURFL API package here (Make sure to download the 1.1 version).
Extract the entire WURFL folder in the package to your /app/vendors folder.
Create a directory /app/configs/wurfl, and extract everything in the resources folder to that directory.
In the examples/resources directory, extract the file in the wurfl-regression.zip to your /app/configs/wurfl directory.
Edit the wurfl-config.xml file to look like the following:
Download code
<?xml version="1.0" encoding="UTF-8"?>
<wurfl-config>
<wurfl>
<main-file>wurfl.xml</main-file>
<patches>
<patch>web_browsers_patch.xml</patch>
</patches>
</wurfl>
<persistence>
<provider>file</provider>
<params>dir=../../tmp/cache/wurfl</params>
</persistence>
<cache>
<provider>null</provider>
</cache>
</wurfl-config>
Create the folder /app/tmp/cache/wurfl.
The Component
Create a file /app/controllers/components/mobiledetect.php and put the following in the file:
Component Class:
Download code
<?php
class MobiledetectComponent extends Object
{
var $isMobile = false;
function startup(&$controller)
{
$this->controller =& $controller;
}
function detect()
{
App::import( 'Vendor', 'WURFL', array( 'file' => 'WURFLManagerProvider.php'));
$wurflConfigFile = CONFIGS.'wurfl/wurfl-config.xml';
$wurflManager = WURFL_WURFLManagerProvider::getWURFLManager($wurflConfigFile);
$requestingDevice = $wurflManager->getDeviceForHttpRequest($_SERVER);
if($requestingDevice->getCapability('is_wireless_device') == 'true')
{
$this->isMobile = true;
$this->setMobile();
$this->controller->theme = 'mobile';
$this->controller->layoutPath = 'mobile';
}
}
}
?>
In your app_controller, remember to include the component like this:
Download code
var $components = array('Mobiledetect');
and add to the beforeFilter function the following:
Download code
function beforeFilter()
{
$this->Mobiledetect->startup($this);
$this->Mobiledetect->detect();
}
And thats it! Now you can create a custom layout file for your mobile website, located under /app/views/layouts/mobile.
You also need to put your css in a directory in /app/webroot/themed/mobile/css
and (optional)images in /app/webroot/themed/mobile/css/img.
Your views need to go to /app/views/themed/mobile.
Your done! Now you can create a mobile version of your website.
Comments
Comment
1 Using android phone to test the function ! - Error !
Comment
2 Scratching the surface
People tickled by mobile detection should do this:
debug($requestingDevice->getAllCapabilities());
That will print almost 500 pieces of information about the device. (logging it is probably better)
To get the benefits worth the "cost" of a heavy data-set like the wurfl database (current version is 14,5MB) you probably want to optimize things like images, video conversions, applications types or any other "content" presented to the mobile browser.
When fully leveraged, Wurfl is really powerful.
Btw: The "real" database is located and updated at: http://wurfl.sourceforge.net/ The one mentioned in the article is a limited test database. Remember to empty the cache after updating the wurfl xml.
Comment
3 Package?
Comment
4 Cake
-----------------
Aaron of sell gold
Comment
5 nice set up
Comment
6 Good
Freestyle Medela
Comment
7 Capabilities are Available to the Server
Removalists4u
require_once('./TeraWurfl.php');
$wurflObj = new TeraWurfl();
$wurflObj->getDeviceCapabilitiesFromAgent();
// see if this client is on a wireless device
if($wurflObj->getDeviceCapability("is_wireless_device")){
header("Location: http://www.removalists4u.com.au");
}
?>
Comment
8 WURFL API
Question
9 WURFL PHP API
What I want to ask is, how do I update the cache? Can I use the old *update_cache.php*?
Electricians4u
Comment
10 Thanks for.........
Thanks for generously sharing so many useful tips.
I am about to begin a blog and this will help me tremendously.
Of course it has given me many things to consider as I update my website too.
I appreciate it.
Calvin00
Comment
11 A couple of issues here
It doesn't seem like anybody has actually tested this code, because the component code that is posted doesn't work out of the box. First the call $this->setMobile() is an illegal call because that function doesn't even exist. Second you need to set the view variable of the controller in order to use the new mobile theme. So in the if clause in the detect() function insert this:
$this->controller->view = 'Theme';
That should get you up and running. Now on to the vendor code. The latest release 1.1/1.r2 is broken - I don't know why it's posted. It died on a missing argument error. The 1.1/1.1r release works, but is about a year old now - is that why it doesn't know about android?? I noticed the wurfl.xml file in 1.r1 is much smaller than the wurfl.xml file in 1.r2. Can I take the one from 1.r2 and drop it in the 1.r1 file structure?
Does anybody know of a better wireless detection php vendor?
Andy
Comment
12 Update WURFL
Comment
13 A First Look at the New WURFL API for PHP
By way of a refresher, WURFL is a "Device Description Repository" - a huge open-source XML-based database of information regarding mobile handsets and their capabilities. I've discussed WURFL in the past, for example here. Prior to this release, the only practical method of querying WURFL in real time from PHP was via a library named Tera-WURFL, which I blogged about here. In fact, both WURFL and Tera-WURFL were covered in an article I wrote for php|architect magazine last year.
Cleaners4u
Comment
14 Thank you
Dog Training Tips
Comment
15 thanks
Comment
16 Give it a try
Comment
17 Thank you
Sheesham Furniture
Comment
18 Thanks
Designer Resources, Php Tutorial
Comment
19 Great Tips!
Research paper writing | custom research paper
Comment
20 Good application
micromax bling
Comment
21 Thank you
Royal Navy Careers
Comment
22 thank you
images upload- Resim yukleme Images upload and resim yukleme sitesi.
Comment
23 Thank you
youtube giris, Youtube Watch, download, Youtube izle, indir dizi izle, ddizi, diziizle.net
Comment
24 abe
Comment
25 Web Conferencing
Comment
26 pool filters
pool filters
Comment
27 DUI Lawyer
have gained some useful information from this site.I admire the
valuable information you offer in your articles. I will bookmark
your blog and have my friends check up here often.
DUI Lawyer
Comment
28 Fantastic!
Pepper Spray
Comment
29 gxdsgdfgfdgf
Comment
30 gsdgdsgdsg
tramadol online buy tramadol tramadol biovail tramadol buy consultation free hydrocodone online tramadol pain medication tramadol can i take tramadol 180 buy tablet tramadol killer pain tramadol effects of tramadol buy information tramadol as carisoprodol get pain tramadol hci tramadol ultram buy diet hoodia now pill tramadol dog medication pain tramadol tramadol prescribing cheap cod fre tramadol ultracet tramadol tramadol prescribing information cheap day mg next tramadol agcode tramadol tramadol recreational cheap online pharmacy tramadol tramadol 180 tablet long term tramadol buy online soma tramadol pain tramadol lowest prices for tramadol online buy online tramadol tramadol hci cure pain medical information on tramadol hc buy now tramadol tramadol pain medication no perscription tramadol buy drug tramadol tramadol pain medicine no prior perscription tramadol buy cheap tramadol hydrochloride ultram tramadol pain relief rx tramadol buy cheap tramadol online purchase tramadol
Comment
31 hello