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:
- Call the checkmobile script
- Then check where you have come from.
- 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.
- 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 highly encouraged to find
I highly encouraged to find your blog. I want to thank you for writing amazing post. I really enjoyed every bit of it and I have your blog bookmarked on my to read new stuff you share.
Amazon Promotional Code
web engineering
So good to see out anyone with some original ideas on this subject. really thank you for beginning this up. this web site is something that’s needed on the web, someone with a little originality. useful job for bringing one thing new to the web.
Web Directories I am aware
Web Directories
I am aware that it would be smart to create a disk module, but have no time now for the code. If someone does, I have some ideas on some features,
Magento themes
All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts. Thanks
Thank you Matt for this code.
Oh, yeah.
Matt - you the best! Finally I foun this function, thank you for your article.
Subscribed to your RSS))
Dmitry from iphone application development company.
I hope you'll keep posting
I hope you'll keep posting relevant information this is what I like to read. I will post a link to this post on my blog keep up the good work!
Online Business Listing
Question
Do you know any other good blogs where i can find a such informative articles?
Thanks for increase my
Thanks for increase my knowledge,I really recommended your style of posting And I very like it; I am very surprised to see this type of blog, once again Thanks for share flower shop netherlands
Does your blog have a
Does your blog have a contact page? I'm having trouble locating it but, I'd like to shoot you an email. I've got some recommendations for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it grow over time.
Second Mortgages
First of all the major
First of all the major website is a drupal 5 website. For the cellular website I determined to use drupal multisites and a subdomain. So generally the cellular website is a absolutely different website.
foam pillows
Automatic mobile device detection in drupal
This really great and it's very useful I am excited to use it . thanks for sharing post.
Sam Stephan from Android Application Developers
Good script
Thanks for the useful script
Alisa Garner from Android Application Development
Mob redirect through php
This method works great (after a little tweaking) - I use it for www.digisolved.com and it works great!
Re: Auto Mobile Detection for Drupal
This was very helpful to me as I wanted to detect the mobile user and send him to the mobile version of my website. Thanks for sharing this information.
Regards,
David from Android Development, iPhone Development and Mobile Development Company
mulberry bags mulberry
mulberry bags
mulberry alexa
mulberry
I am planning to give a
I am planning to give a presentation on "Data Mashups" using QueryPath and XSLT in a very general way - with not very much code - mostly what you can do with QP and XSLT, and why I think "Data Mashups" are an important part of the future for Drupal.
I also plan to show "newbies" how the use of just three contrib modules: Views, Panels and QueryPath can give you unlimited flexibility in creating data mashups with data from external sources.
I therefore suggest that I go first, as I think this will be useful and important information for people who are new to Drupal, before we break out in different groups.
beegbbc
Hello! dkdbeeb interesting dkdbeeb site!
afkkbke
Hello! cdfekaf interesting cdfekaf site!
good
Hi, I found this blog post while was searching for sports related information on bing and found it very good article, thanks for sharing, best regards and cheers!
dell laptop parts
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)
I am really happy to listen
I am really happy to listen that The Mobile Device Detection module allows a site owner to handle the redirection of his users to his mobile site. This module does not a transformation of your site to a mobile version, but it assist you in handling the traffic to your mobile and desktop site. You can choose the most user friendly way of redirection your Visitors.
Mark Taylor@testking 642-533
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.
Programmers are facing many
Programmers are facing many challenges one of them is Robot designing.Embedded systems are playing very important role in our life.Now a days I am working on web bot projects and its awesome.
Artikelverzeichnisse Good
Artikelverzeichnisse
Good news adminThank end users for send and receive in truth great informations. Your New internet is greatI am impressed by the advice which buyers have by use of this website. it shows Tricks to great buyers know your title. Bookmarked this url, went go back for way more. consumers, my acquaintance, ROCK! I found just The instructions I already searched everywhere and also only couldn't experience. What a perfect website. liking things like this web site A New web site is I of my new favs.I liking things like this info shown as well as it without doubt has given me A few kind as for desire To succeed being Some rationale, awfully carry build up The powerful work
Which programmers
How do you Edit the settings.php file for the main domain in this case it was the sites/default/settings.php?