This topic is locked

WURFL mobile user agent detection solution

8/16/2011 6:21:30 PM
PHPRunner Tips and Tricks
F
FunkDaddy author

Apparently PHPR 6.0 is purported to include automatic user agent detection in order to route incoming requests to the correct mobile version of your application. However, since the newly released version is still in Beta and lacks any documentation on how it handles this endeavor, I decided to write this post to help folks handle this on their own.
The method I chose to employ was to make use of WURFL (Wireless Universal Resource File) as it appears to be the most commonly recommended resource out there.
More info on WURFL here:


I chose to implement the Database version of their API (as mentioned on the 3rd link shown in the list above). I originally tried using the simpler PHP API listed, but ran into some difficulties during setup and thus decided to stick with relying on MySQL db's instead (my comfort zone).
Here are the steps to implement and setup:
Step 1:

Download the tera-wurlf package form here: http://dbapi.scientiamobile.com/wiki/index.php/Downloads
Step 2:

Follow the steps outlined in the DB API documentation here: http://dbapi.scientiamobile.com/wiki/index.php/Installation
Step3:

Per their instructions copy the file named TeraWurflConfig.php.example (found in the root of the extracted folder you've just downloaded) and rename it to simply TeraWurflConfig.php
Alter that new file so that it contains your database username, password, host, etc. (in my case I was using MySQL 5.1 via Navicat... thus, I made sure to create a user with the right permissions to the connections I was planning on declaring in the TeraWurflConfig.php file)
TIP... I made sure the user privileges assigned in Navicat were also assigned to my DB on my remote web server (I have a localhost db and a copy I keep in sync on a live web server) so that the script package would be easily copied-n-pasted onto the live server once I was ready to deploy).
Step4:

Per their instructions, I opened my web browser and went to: "http://yourserver.com/Tera-Wurfl/admin/install.php"'>http://yourserver.com/Tera-Wurfl/admin/install.php"'>http://yourserver.com/Tera-Wurfl/admin/install.php"'>http://yourserver.com/Tera-Wurfl/admin/install.php"; (in my case "yourserver.com" was my "localhost" machine).
Upon running that script in the browser my MySQL DB was populated with 45 newly created tables and over 14,000 device profiles (you should read about their 2 step user agent sniffing process to understand why they've broken up the schema this way).
Step 5:

I created a test script using the example they provided in the documentation and placed it inside the root folder of one of my published projects in my localhost folder to test. Sure enough everything worked as expected: I was able to swicth user-agents using FireFox's "user agent switcher" extension and the results were spot-on!
Here is the sample code they provided:



<?php

// Include the Tera-WURFL file

require_once('./TeraWurfl.php');



// instantiate the Tera-WURFL object

$wurflObj = new TeraWurfl();



// Get the capabilities of the current client.

$matched = $wurflObj->getDeviceCapabilitiesFromAgent();



// see if this client is on a wireless device (or if they can't be identified)

if(!$wurflObj->getDeviceCapability("is_wireless_device")){

echo "<h2>You should not be here</h2>";

}



// see what this device's preferred markup language is

echo "Markup: ".$wurflObj->getDeviceCapability("preferred_markup");



// see the display resolution

$width = $wurflObj->getDeviceCapability("resolution_width");

$height = $wurflObj->getDeviceCapability("resolution_height");

echo "<br/>Resolution: $width x $height<br/>";

?>


Of course, I went ahead added some additional lines of code to test what I could pull from the $wurflObj array. You can go here to find out what can be retrieved from each device (assuming the db has it for a given device): http://www.scientiamobile.com/wurflCapability/tree
Remember, the code above is merely to test whether you have things working correctly. Make sure the include line in the script points to the correct location in relation to your test script.
Step 6:

Deploy this package to your live web server.
Now for the tricky part.... (at least for me since this is the problem that I ran into)...
When I uploaded my tera wurfl folder to the web server and ran the "http://yourserver.com/Tera-Wurfl/admin/install.php"'>http://yourserver.com/Tera-Wurfl/admin/install.php"'>http://yourserver.com/Tera-Wurfl/admin/install.php"'>http://yourserver.com/Tera-Wurfl/admin/install.php"; nothing happened! No error messages, no exceptions, nothing. All I got was a blank white screen. Inspecting it with Firebug revealed no anomalies whatsoever. Additionally, no tables were created in MySQL server as was the case when I ran that script on my localhost.
After searching through their help forums (http://www.tera-wurfl.com/forum/viewtopic.php?f=2&t=120&hilit=blank) I was able to solve the issue by doing the following (remember, if you face this problem it may be due to a different issue, so make sure you look at the trouble shooting section initially shown right along with the db api documentation):
Step 1:

It turns out that when I ran my <? info() ?> function on my web server I was lacking mysqli extension. Thus, I had to uncomment this line "extension=php_mysqli.dll" in my php.ini file (remove the semicolon).
Step 2:

Depending on your configuration and environment (windows, linux, etc), you may need to install the supporting DLL for the extensions. If so, here's how you do it: http://us.php.net/manual/en/mysqli.installation.php
Step 3:

That's it. All you need to do now is restart your IIS application pool (since I my web server is Win2K8 - which I did via a Plesk control panel recycle action) for the php.ini file to take effect.
Then run "http://yourserver.com/Tera-Wurfl/admin/install.php"'>http://yourserver.com/Tera-Wurfl/admin/install.php"'>http://yourserver.com/Tera-Wurfl/admin/install.php"'>http://yourserver.com/Tera-Wurfl/admin/install.php"; in your browser and your db should populate with newly created tables and records.
By the way.. notice that I created a standalone folder called "Tera-Wurfl" which is where I kept all of the files provided by their package. I created this folder in the root of my site's application to keep it separate from all of my files and folders output by PHPR. Keeps things more organized and simpler to reference.
Step 4:

Lastly (I almost forgot)... we include some code in our "Login Page: Before Process" event of PHPR to handle the redirection based on mobile detection:



// Include the Tera-WURFL file

// Make sure you point this accordingly... it all depends where this file is in relation to your application's login.php page.

require_once('../Tera-Wurfl/TeraWurfl.php');



// instantiate the Tera-WURFL object

$wurflObj = new TeraWurfl();



// Get the capabilities of the current client.

$matched = $wurflObj->getDeviceCapabilitiesFromAgent();



// see if this client is on a wireless device (or if they can't be identified)

if($wurflObj->getDeviceCapability("is_wireless_device")){

header("Location: https://yoursite.com/mobile/login.php";);

exit();

}


Notice that I've made the coding above overly simple just for demonstration purposes. You will need to build your own coding logic to expand on what you want to do with devices based on their user agent.
Final Observations & Tips:
I opted to create a separate DB entirely in MySQL server named "tera_wurfl" so that I could reference it from multiple applications on multiple domains hosted on that same machine (since they share the same localhost connection to the MySQL server). This also kept the 45 tables in a separate DB instead of cluttering my existing DB, which makes it easier for me to manage, backup, etc.
Don't forget to follow their 2 suggested security steps from their dcoumentatin:



Security

You should delete the admin/install.php file if everything is working ok.

Protect your 'admin' directory by using a method specific to your web server software. For Apache, this means a .htaccess file would work.


Cheers,

K
kenlyle 11/15/2011

Nice tutorial. Thanks!