Automatic mobile device detection in drupal.

I have previously set-up automatic device detection for mobile devices for a domain 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.yourdomain.com 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 multisite I just pointed the subdomain to the appropriate drupal multisite.

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.

Im sorry but I do not think

Im sorry but I do not think this would work. I would want to try this but afraid my phone might cause some trouble shoots after trying.... I guess it would be more easier and safer to just go directly to any of the phone dealers.. to help you.

Wow thanks for the great

Wow thanks for the great article. Nowadays, when everyone have the mobile phone, we must consider about making one more version of the website just for mobile users. It must have less scrolling, less weight images and so on. In other words it must be more comfortable for mobile users to browse. Thanks for this useful script, I will definitely integrate it in my Drupal websites. Oh and I will be waiting for more interesting entries from you in the future too.

Sincerely,

Matt Dammson from mobile application development

Redirect the mobile site back to the main site

HI Could you please let me know the same in ASP redirect back from mobile site to the main site.
I have auto redirected to the mobile from main site, now in the mobile site i have link in the footer wherein clicking the link it has redirect back to the main site. Both Main site and mobile site are from two different application.

Any help would be appreciated

er

But all that is just about over now and things are happening at a fast pace. My new hospital will be opening very soon and I've just finished building the practice website- well for now anyway...

comment

Hello, i am cliff with you and today i will disclose some new authentic medicines and i hope these are really effective. cialis are completely without any side effects.

Update info for Apache Mobile Filter

The "Apache Mobile Filter" is one of the modules of "Apache Module Registry" portal (http://modules.apache.org/search.php?id=1787)

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.