Detecting mobile devices on your website

PHP Add comments

The purpose of this post is to introduce the concept of detecting mobile devices on your website using an open source library called wurfl, The library can be deployed using the following programming languages:

  1. 1C++
  2. Dot Net
  3. Java
  4. Perl
  5. Python
  6. PHP
  7. Ruby

I will be using PHP for all the examples on this post, So I hope you have a little understanding of PHP to go through some of the example code.

Requirements for this Exercise:

  • At least 50 mega bytes of storage(for the wurfl files to be generated).

Step 1:This step can be set up in different ways, It all depends on how you want to use wurfl on your project, I personally use it on more than one project. To avoid creating a wurfl installation for all my projects I have had to set up a global wurfl installation on my web server which allows all my projects to use one wurfl instance.

create wurfl global

go to /www and create a folder called /www/global and inside global create another directory called /www/global/wurfl. We need to create one last directory inside our /www/global/wurfl/ directory which we are going to call /www/global/wurfl/data, Before we close this step please make sure that your directory /www/global/wurfl/data is writeable

chmod 0777 /www/global/wurfl/data

NB chmod 0777 is not recommended on a production server and one must be careful when granting write permission to files. For the sake of the demonstration we will ignore this rule though.

Your directory structure should look like this: /www/global/wurfl/data

Step 2:
Go to wurfl download page and get the PHP files and store them inside /www/global/wurfl directory http://wurfl.sourceforge.net/php/index.php
you need to also download the http://wurfl.sourceforge.net/wurfl.xml(7mb) which contains all the information about the different types of phones in the market. This file is updated constantly and you need to update it on your side regularly. This file must be copied to /www/global/wurfl/data/wurfl.xml

Step 3:
After you uncompress the downloaded PHP files you should have these following files available:1.

  1. check_wurfl.php
  2. update_cache.php
  3. wurfl_class.php
  4. wurfl_config.php
  5. wurfl_parser.php
  6. readme.txt

Step 4:

Now lets configure our library by opening /www/global/wurfl/wurfl_config.php
lets change the config file as shown below.

Line 90:
NB: Make path absolute

// Where all data is stored (wurfl.xml, cache file, logs, etc) define(”DATADIR”, ‘/www/global/wurfl/data/’);

Line 93:
// Path and filename of wurfl_parser.php define(”WURFL_PARSER_FILE”, ‘/www/global/wurfl/wurfl_parser.php’);

Line 96:
// Path and filename of wurfl_class.php define(”WURFL_CLASS_FILE”, ‘/www/global/wurfl/wurfl_class.php’);

That will do for now. If you are interested in some of the configuration settings on this file you can consult the documentation for the library.

Step 5:
We now need to run /www/global/wurfl/update_cache.php on the console which will create a folder called /global/wurfl/data/multicache that has a lot of files inside of it.

/usr/bin/php -f /www/global/wurfl/update_cache.php

After you run the command it will create the folder mentioned above and a file called /www/global/wurfl/data/cache.php. This command will take a while to execute depending on your machine but will take between 30 to 60 seconds to execute. My /www/global/wurfl/data directory’s size is now 37 mega bytes.

Step 6:
Lets write a little test code which determines if the device on our site is a mobile device and if so we can then redirect the device to the mobile version of our site.


Just like that we now have a script that can tell us whether the user is using a computer web browser or phone browser. If the phone is picked up by our script $myDevice->brand = nokia will carry the phone brand while $myDevice->model = N70. But Please remember that this method is not 100% accurate and also keeping an updated version of /www/global/wurfl/data/wurfl.xml will increase the accuracy rate.

$wurfl_class->capabilities holds an array of the device’s capabilities e.g.
can the device support gif images?

Please try and explore the documentation for the library and you will find all sorts of features. For the sake of keeping this post small I will not show any more examples but rather encourage you to read up a bit on this beautiful library. I hope you find this post useful and you get to do magical stuff with this library , Feel free to leave a comment.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
Tags: , , ,

4 Responses to “Detecting mobile devices on your website”

  1. dave Says:

    have you tryed handsetdetection.com?

    It detects mobile devices on your website and then returns data like mobile phone browser, mobile phone screen size etc…

  2. Free Gadget Says:

    Thanks for the information, bookmarked your page for updates :)

  3. Convert Flv Says:

    No doubt FLV format has quickly established itself as the format of choice for embedded video on the web.

  4. gold Says:

    Sehr interessant. Kommt hier noch ein weiterer Beitrag? Würde gern mehr darüber erfahren. Kannst du mir per E-Mail eine Antwort geben?

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in