Automatic mobile device detection in drupal.

I have just finished setting up automatic device detection for mobile devices for crownvet.com.au so thought I would document how I did it here as I had alot of trouble finding information on how to do it. I had considered using the accessability module but it didn't really do what I wanted and seems far too complex. I think this way is much easier!

The basic setup

First of all the main domain is a drupal 5 site. For the mobile site I decided to use drupal multisites and a subdomain. So basically the mobile site is a completely different site.

I tried to think of the context in which a user who is mobile would be accessing the site and what information they would want. s they are mobile they are likely to have a slow connection, want quick information about the business, the business location and contact details. It is unlikely that they would be looking for information on diseases for instance so there is no need to include that in the mobile site.

I decided on m.crownvet.com.au as the subdomain as sticking an m in front is common practice now with larger sites on their subdomains so people are likely to guess it and it's quick and logical. Once I had created the drupal mutlisite I just pointed the subdomain to the appropriate drupal mulitsite.

Detecting if a device is mobile.

For drupal, a php script seemed the logical solution to detect whether a device is mobile or not. There are a couple of scripts floating around but the one I decided to use was one posted by Greg Bulmash on Brain Handles. So What I did was to create a php file called checkmobile.php and insert into it the following code:

function checkmobile(){
if(isset($_SERVER["HTTP_X_WAP_PROFILE"])) return true;
if(preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) return true;
if(isset($_SERVER["HTTP_USER_AGENT"])){
$uamatches = array("midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto");
foreach($uamatches as $uastring){
if(preg_match("/".$uastring."/i",$_SERVER["HTTP_USER_AGENT"])) return true;
}
}
return false;
} 

I then uploaded that php file to my sites/all directory.

So, how to call the detection script and then redirect?

Edit the settings.php file for the main domain in this case it was the sites/default/settings.php. In this file as the very first thing following the line with the php tag, add the following snippet:

include('sites/all/checkmobile.php');
$from = getenv("HTTP_REFERER");
if ($from == "http://m.yourdomain.com/")
/* If refering domain is your mobile domain we don't want to send them back there so do nothing*/
{}
else
{if(checkmobile()) header("Location:http://m.yourdomain.com");}

You will need to change m.yourdomain.com to whatever your mobile domain name is.

So this snippet will:

  1. Call the checkmobile script
  2. Then check where you have come from.
  3. If you have come from the mobile site already it won't execute anything and just continue on to load the main domain site. This is so if you have a link on the mobile subdomain to the main domain it will actually follow it and not just redirect back to the mobile subdomain again. Eg. If you have a link to view the main site incase a user has a decent web browser or has been wrongly detected as mobile they can then follow a link to the main domain site.
  4. If the referring site is not the mobile subdomain the script checks if you are mobile and if so redirects to the mobile subdomain.

I hope that this helps someone out there. If you have a mobile device that does or doesn't work with this method I would appreciate your feedback. So far I have only tested it on my iphone and on the opera mini simulator.

I have enhanced this method using cookies to allow the user to continue browsing the main domain and not get redirected back to the mobile subdomain.

Apache Mobile Filter

I have published the new version of Apache Mobile Filter, now the filter is give to you the information of capabilities as apache environment.
Now you can develope in any language (php,jsp, ruby etc.) and have the information of mobile capability.

Read more info here: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html

Hi Thanks for posting

Hi

Thanks for posting this... is there a way to simply place this type of code into the header of an index.php (homepage) which then redirects from the root of the website rather than having to mess around with the server settings?

Thanks

Steve

Hi Steve

I don't believe that would work. The script needs to be called on each page. Otherwise if someone enters your site from a page other than the index.php page their browser would not be detected.