dotMobimobiThinkingmobiForgemobiReadyDeviceAtlasgoMobi
background image

Lightweight Device-Detection in PHP

Multiple glasses
Posted by ronan - 01 Jun 2007
Twitter share icon Facebook share icon Google Plus share icon

One problem that keeps cropping up when developing mobile content is how to differentiate between mobile devices and desktop browsers. We need to be able to do this before we can think about content adaptation. The "proper" way to do this is to use a full device detection database such as DeviceAtlas (see our tutorial here) but the following method may be sufficient to get you started.

Update: We have an improved version of the algorithm which includes tablet detection here.

This PHP code implements a mobile device detection algorithm that works for a good percentage of mobile browsers out there. This code is the work of Andy Moore. The algorithm used is fairly lightweight—the code is mostly based on a list of about 90 well-known mobile browser User-Agent string snippets, with a couple of special cases for Opera Mini, the W3C Default Delivery Context and some other Windows browsers. The code also looks to see if the browser advertises WAP capabilities as a hint.

In our experience, this code does a fairly good job. It could probably be improved but it's certainly not a bad start, and is lightweight enough not to cause major problems. For more accurate detection and increased performance we recommend using a dedicated device detection such as DeviceAtlas. Doing so will result in much higher performance, reduced load on your server and much better accuracy.

<?php
 
$mobile_browser = '0';
 
if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
    $mobile_browser++;
}
 
if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
    $mobile_browser++;
}    
 
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
$mobile_agents = array(
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
    'wapr','webc','winw','winw','xda ','xda-');
 
if (in_array($mobile_ua,$mobile_agents)) {
    $mobile_browser++;
}
 
if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) {
    $mobile_browser++;
}
 
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
    $mobile_browser = 0;
}
 
if ($mobile_browser > 0) {
   // do something
}
else {
   // do something else
}   
 
?>

The changes changes made from Andy's original version are:

  • Adding the W3C User-Agent string (Default Delivery Context)
  • Special case detection for Opera Mini
  • Catch-all exception for devices with Windows in the UA string (Opera 9 for Windows was being recognised as a mobile device)
  • Android User-Agent snippet added

If anyone has any improvements on this code, or implementations for other languages, please let us know!

See also an ASP version.


Posted by ronan - 01 Jun 2007

ronan's picture

« m o b i l i s t , CTO of dotMobi » @xbs

Posted by pbateman 6 years ago

Would this not be better placed in a function returning true as soon as it has detected it's a mobile device to prevent it continuing its way through the rest of the code

Posted by eduardoraad 6 years ago

I ported the code to Java and added some simple stuff. I will be adding more features later. You can download it from Google Code:

http://code.google.com/p/mobiledevicedetector/

Posted by fritzbrause 6 years ago

Hi there!

thanks for the nice little code. I tried WURFL, to be sure, that it is a mobile device, which visits my page, but WURFL fails on Windows Mobile Smartphones. So i try this piece of code, but it also dont recognize my Windows Mobile Testphone ;-)
The Problem lies in the last check, which overides all others:
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) { $mobile_browser=0; }

Windows Mobile (and older Versions of the Windows CE platform for PocketPC and Smartphones), use a small Internet Explorer for browsing. Which send an agent like this:

HTC_P3300 Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 7.6)

This example shows the default Agent of a HTC P3300 Smartphone with Windows Mobile 6.
As there is a "Windows" in the text, the code above detects a desktop-system.

To make it easy (this time) i just added a new line (just below the one above), which fixes this problem (hopefully nobody uses an opera mini on a Windows smartpohne :):

if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'iemobile')>0) {
$mobile_browser++;
}

Regards!
Markus

Posted by spellboy 6 years ago

To detect the opera mobile browser (and many other) you have to add the following code:

	if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),' ppc;')>0) {
		$mobile_browser++;
	}

Kind regards,
Christopher

http://m.spellboy.com - your mobile spell checker

Posted by hidayath 6 years ago

any one have ASP version of this code

hidayath
www.MobiBlaze.com

hidayath www.MobiBlaze.com

Posted by ruadhan 6 years ago

We've recently added an asp version here.

Ruadhan O'Donoghue
Editor, mobiForge

Posted by promorock 6 years ago

What is the best way to implement this?

Jonathan Morgan PromoRock www.promorock.com

Posted by ruadhan 6 years ago

You just need to include this code at the top of your PHP page. Then at the end of the script you will see we have to options: mobile or not-mobile. In the branch for mobile you can put your PHP code for your mobile page, and in the not-mobile branch you put in your non-mobile content.

Does this help?

Ruadhan O'Donoghue
Editor, mobiForge

Posted by kimurray 6 years ago

Greetings: Can I include that php code on the html index page of my website? You have already guessed I am a code dummy and dont savvy php at all!
If I can where should it go and what code do I use to redirect to my .mobi site please.

Posted by ruadhan 6 years ago

Yes, probably the most simple way would be to include at the top of your index page, and then send the user to the appropriate content.

Another approach would be to have the code at the top of every page, so whatever the entry point, the user would get the right page

Ruadhan O'Donoghue
Editor, mobiForge

Posted by Roberto Trama (not verified) 6 years ago

There is also another problem in the code, which I tested on my website and hidden by the overriding check fo the string containing "windows": the array to recognize mobile user agents includes the string "oper" that will cause to consider as mobile device some Opera browsers. The check for Windows browsers will exclude Opera for Windows, but not Opera for Linux. The HTTP_USER_AGENT: Opera/9.50 (X11; Linux i686; U; en), as example, will redirect to the mobile version of the website. I have excluded the string "oper" from the array and now redirection does not occurr for all Opera browsers on desktops. Which other mobile browsers (exluding Opera) begin with "oper"?

Posted by geigers 6 years ago

Thanks for this little piece of code, it's just what I needed. Still working on testing it out. I needed to re-write it in PL/SQL (Oracle Stored Procedure Language) for an application I am working on. I did find a problem with this section of the code:

if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) {

It will never evaluate as true. An "strtolower" is done on $_SERVER['ALL_HTTP'] and then an "strpos" is run to compare against "OperaMini" <--- notice there are CAPS in the compare value (O and M). Should be:

if (strpos(strtolower($_SERVER['ALL_HTTP']),'operamini')>0) {

Scott Geiger

Posted by surin 6 years ago

Its very useful program, i really appreciate the devi.mobi for describe it and provide it for me. i again thanks for this, please keep it updating according to new changes in mobile environments.. thanks

Posted by majorsoft 6 years ago

Thx - good knowledge.

Is there anyone out there that can help me setup a small site - with mobile access to detect device and provide a specific version of a jar file for download?

Looking for a mobile turnkey solution that does not require week to install and configure.

Any ideas?

Thx again

Major

Posted by anthonyhand 6 years ago

Hi!

I just published a PHP script for detecting mobile devices on my web site:

www.hand-interactive.com/resources/detect-mobile-php.htm

The code is very flexible with a fairly rich API, can detect specific platforms (e.g., iPhone vs. Symbian S60), or classes of devices (e.g., smartphones vs. generic mobile device), and more. The code is downloadable and freely shared under a Creative Commons license. (Though a donation is gratefully accepted!)

I've also published some JavaScript code which closely mirrors the PHP one, except that the JavaScript library is of limited utility (due to limited JavaScript support in browsers on mobile devices.)

Please take a look at it. Extensions and fixes greatly appreciated, as well!

Cheers,
Anthony




*************************************************************

Anthony Hand,
Freelance Mobile User Interface Designer

w: www.hand-interactive.com

*************************************************************

Posted by cwbash 6 years ago

I've got this code running and find everything fine down to the line which says
if($mobile_browser>0)
do something

What I'd like to do at that point is redirect this link to another page. I'm told from other articles that the solution is to use "header (Location:", but when I do so, I'm told "Cannot modify header information - headers already sent".

There's nothing in my php script except the php code (line 1 is the "<?php" so the classic example of inserting HTML at the beginning hasn't been done. Any thoughts about why headers have already been sent? I would like suggestions about how to "do something" that can stay on the server. If I can't, can you supply code that would set an HTML or Javascript variable so just a few lines on the client would do the redirect?

You can see my full code at http://www.lighthousesrus.org/mobile/test.php

Posted by robman 5 years ago

Hi Ronan,

I'd be interested in what you (and the people that are using your script) think of the discussion I've started with James (see my post here).

I think there's some real benefits in turning your approach around the other way - we call this the "Not-Device" Detection Strategy.

From our experience over the last 2 years this provides better coverage and better performance, without the maintenance issues as new devices are released.

roBman

Posted by pascalv 5 years ago

I used the original code, and changed the comments given above.

Also I changed the detection for "windows". If there is a "Windows CE", then it doesn't have to look further.

My new changed code:

$mobile_browser = '0';
 
if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
    $mobile_browser++;
}
 
if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
    $mobile_browser++;
}    
 
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array(
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
    'wapr','webc','winw','winw','xda','xda-');
 
if(in_array($mobile_ua,$mobile_agents)) {
    $mobile_browser++;
}
 
if (strpos(strtolower($_SERVER['ALL_HTTP']),'operamini')>0) {
    $mobile_browser++;
}
 
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),' ppc;')>0) {
	$mobile_browser++;
}
 
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows ce')>0) {
	$mobile_browser++;
}
else if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0) {
    $mobile_browser=0;
}
 
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'iemobile')>0) {
	$mobile_browser++;
}
 
if($mobile_browser>0) {
   // do something
   // YES MOBILE
}
else {
   // do something else
   // NOT MOBILE
}

Posted by torweb 5 years ago

I have a version of this working but the above looks better. My question is how do I complete the code...the part:

if($mobile_browser>0) {
// do something
// YES MOBILE
}
else {
// do something else
// NOT MOBILE
}

I'm new at this....

Thanks

Posted by onesearch 5 years ago

Hi, Dont want to sound stupid, but, New to PHP and was wondering the same as the last message, What do i replace/add to complete the code.

Thanks in Advance

Posted by magicman2000 5 years ago

has anyone tried a web service like handsetdetection.com? I think that it is PHP friendly

Posted by JMANJ 5 years ago

Thanks for this code ! After some tests we identified a problem with Macintosh browsers such like Opera.
For example, the UserAgent of a typical Opera installation on Mac OS is : "Opera/9.63 (Macintosh : PPC Mac OS X : U : fr) Presto / 2.1.1"

In this case, the code identifies the browser like a mobile_browser which is not the case. It comes from the $mobile_agents array which contains the string "oper". So each Opera browser on Macintosh is seen like a mobile browser (not the on Windows because of the 'windows' test).

Any idea on a patch to avoid that (deleting "oper" string doesn't look like the best solution) ?
Thanks in advance,
Jmanj

Posted by EseMismo 5 years ago

Hi,

I've been reading recently some posts, and maybe a little explanation 'bout basic app architecture could bring some light - where to go, and how to take advantage of the "location" function.
This post has a twofold goal:
* Show an explicit URL to go (wether mobile or not)
* Overcome the infamous "Cannot modify header information - headers already sent" error message ;-)

What you need is a page that looks something like this:

<? PHP
 
/*
Put here any PHP code - i.e. the detection script
AND/OR any functions, includes, etc.
       ...
       ...
*/
 
/*
Now, it's **VERY IMPORTANT**
This holds the entire page's output buffer, until it's time to SEND HEADERS
*/
ob_start();
?>
 
        <HTML>
        <!-- Your HTML code here, if any - i.e. your CSS, JS and title
               ...
               ...
        -->
        </HTML>
 
<? PHP
if($mobile_browser>0) {
   // YES MOBILE - do something
   header("Location: mobile.php"); // go to mobile URL
}
else {
   // NOT MOBILE - do something else
   header("Location: desktop.php"); // go to desktop URL
}
 
/*
Now, it's **VERY IMPORTANT AGAIN**
This allows headers & content to be sent to the browser
*/
ob_end_flush();
?>

Really, this example could grow a bit more, to become something called "page controller" (look in Google or Wikipedia for more on programming patterns): you have an almost blank "index" page, and depending on some conditions, you execute or call (include or require) one or another "code chunk".
Of course, this is just a skeleton - you should taylor it to your specific needs - but while being basic, it works.
Hope you find it useful.

EseMismo

Posted by john.boxall 5 years ago

Be careful with this kind of thing - you're playing with fire.

This script will always redirect mobile devices to the mobile page - depending on how well you've designed your mobile site and whether you've hidden functionality or not - this may not always be what the user wants.

Play it safe and allow the user a way to force either the full or mobile versions of the site - you can do this easily with an extra GET parameter on requests:

In your HTML always include likes to both versions of the site w/ the special force mode parameters:

<a href="http://www.example.org/?mobile">View Mobile Site</a>
<a href="http://www.example.org/?full">View Full Site</a>

In your PHP always check for the parameters before you do the User-Agent happy dance:

<?php
 
if ($_GET['mobile']) {
 $is_mobile = true;
}
 
if ($_GET['full']) {
 $is_mobile = false;
}

Cheers,

John

P.S

Dotmobi dudes, this input box is super small - we're not typing a tweet here - this is for a post!

Update in my habit of reading the oldest posts first I missed James' post on doing what I described above.

Posted by microgers206 5 years ago

hey guys, i am currently building a buddy of mine a web site for his business, and I am trying to create a mobile site (as his index is a flash intro, and cannot be seen by most mobile devices.) I am extremely new to the mobile device web design, and am not too experienced with php or asp. Can you give me a step by step to introducing this redirect on my site? thank you very much and greatly appreciated.

Posted by ifuschini 5 years ago

Hi,
you can use Apache Mobile Filter and the keep the information of capability as environment variable, I think it's a good solution for work with any language supported with Apache Web Server (Ruby, PHP, JSP, perl).

See the info here: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html

Mobile & Web 2.0 Consultant

Posted by Teli 5 years ago

------------------------

Hello -

The above code does not work and the original code does not work either. Ofcourse, I justed copied and pasted the code but have had no luck and ideas would be appricated.

Thanks for your time,
Teli

Posted by David123 5 years ago

My php script except the php code (line 1 is the "<?php" so the classic example of inserting HTML at the beginning hasn't been done. Any thoughts about why headers have already been sent? I would like suggestions about how to "do something" that can stay on the server

Posted by David123 5 years ago

Thank you for your expression of picture I'll just try my iphone device...

Posted by turbowebguy 5 years ago

Why not just make a landing page with an option: Click here for mobile site
I'm kidding. Great page, cool site, lots of info.

Posted by econn_designer 4 years ago

Hello Everyone

I am new in making mobile based website. My question is :
[i]My website is fully static and made in simple html pages. I have made the mobile version and make its subdomain "www.m.abc.com"
Now is there any script which can redirect the user when he/she open my website on mobile and redirect he/she when seeing my website on desktop

Will wait for your help
Regards

Posted by whoisstan 4 years ago

made a very compact php mobile detection method if you need one:

function is_mobile(){
	$regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
	$regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
	$regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";	
	$regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
	$regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
	$regex_match.=")/i";		
	return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
}

Posted by 1kipp 4 years ago

To WHOISSTAN,
I havent tried your code above yet but i believe it works fine. Now, how is a mobile device redirected? I havent seen any redirecting codes or links, thanks.

Posted by mobiforgestinks 4 years ago

@WHOISSTAN, thanks a million for your concise code. Works as expected.

@1KIPP and @everyone,

To use WHOISSTAN's code, include an IF statement. For example:

if(is_mobile()) { 
//it's a mobile browser, do something
header("Location: http://www.yoursite.com/mobile");
} else {
//it's not a mobile browser, do something else
header("Location: http://www.yoursite.com/desktop");
}

Posted by Daniel2010 4 years ago

whoisstan wrote:

made a very compact php mobile detection method if you need one:

where do i put my html code in this, can you show an example of how i would do this?

thanks,
dan

Posted by vilehelm 4 years ago

This was really helpful. Thanks.

I had an instance where it made sense to push an iPad to alternative mobile content (instead of a flash site) so I added this snippet:

if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'ipad')>0) {
    $mobile_browser++;
}

Worked like a peach.

Thanks again.

Posted by mikelly 3 years ago

HELP! I am new to the mobile platform. I have used a number of codes I have been given to detect and redirect to a mobile site - I like and am using this one. However, it does not seem to wrok with the Droid. But, the HTC does. I do not understand???
Any help would be greatly appreciated...below is a copy of the code on my PHP index page.

<?php
function is_mobile()
{
$regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|"; $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|"; $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cgh|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
$regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|"; $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
$regex_match.=")/i";
return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
}
if(is_mobile())
{
header("Location: http://www.pdxflyers.com/mobile");
}
else {
header("Location: http://www.pdxflyers.com/pc");
}

//Mike K. begin testing Oct 1, 2010 (mobiletest2.php)
//Droid does not work/HTC does on (mobiletest2.php)

?>

PDX Flyers, Real Estate Flyers e-flyers Flyer Templates, Listing Flyers

Posted by rhernandez 3 years ago

Hey, I'm trying with this code but it is not redirecting to my mobile site, Could someone give me an idea why is not redirecting please?

<?php

define('MOBILE_SITE', 'http://www.gm5449.gomobinow.mobi');
define('DA_USE_COOKIES', true); define('DA_USE_CACHE', true);
define('COOKIE_EXPIRY_TIME', 21600);
define('DA_CACHE_DIR', sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'DeviceAtlasCache' . DIRECTORY_SEPARATOR);
define('DA_URI', 'http://detect.deviceatlas.com/query?User-Agent=Mozilla%2F5.0+%28SymbianOS%2F9.2%3B+Series60');

$redirect = isset($_REQUEST['redirect'])?$_REQUEST['redirect']:null;

if($redirect!='false') {
$da_results = array('_source' => 'none');
$prevent_redirect = false;

if (DA_USE_COOKIES && isset($_COOKIE['Mobi_Mtld_DA_Prevent_Redirect'])) {

//Clear the redirect-prevention cookie if we see redirect=true parameter
if($redirect == 'true') {
setcookie('Mobi_Mtld_DA_Prevent_Redirect', 'false', time()-3600);
}
else {
$prevent_redirect = $_COOKIE['Mobi_Mtld_DA_Prevent_Redirect'];
if($prevent_redirect) {
$da_results['_source'] = 'prevent_redirect';
}
}
}

if (DA_USE_COOKIES && isset($_COOKIE['Mobi_Mtld_DA_Properties'])) {
$da_results = (array)json_decode($_COOKIE['Mobi_Mtld_DA_Properties'], true);
$da_results['_source'] = 'cookie';
}

if (DA_USE_CACHE && $da_results['_source'] === 'none') {
$da_cache_file = md5($_SERVER["HTTP_USER_AGENT"]) . '.json';

if (!file_exists(DA_CACHE_DIR) && !@mkdir(DA_CACHE_DIR)) {
$da_results['_error'] = "Unable to create cache directory: " . DA_CACHE_DIR . "\n";
} else {

$da_json = @file_get_contents(DA_CACHE_DIR . $da_cache_file);

if ($da_json !== false) {
$da_results = (array)json_decode($da_json, true);
$da_results['_source'] = 'cache';

if (DA_USE_COOKIES) {
setcookie('Mobi_Mtld_DA_Properties', $da_json);
}
}
}
}

if ($da_results['_source'] === 'none') {
$da_json = @file_get_contents(DA_URI . "?User-Agent=" . urlencode($_SERVER["HTTP_USER_AGENT"]));

if ($da_json !== false) {
$da_results = array_merge(json_decode($da_json, true), $da_results);
$da_results['_source'] = 'webservice';

if (DA_USE_COOKIES) {
setcookie('Mobi_Mtld_DA_Properties', $da_json);
}

if (DA_USE_CACHE) {

if (@file_put_contents(DA_CACHE_DIR . $da_cache_file, $da_json) === false) {
$da_results['_error'] .= "Unable to write cache file " . DA_CACHE_DIR . $da_cache_file . "\n";
}
}
} else {

$da_results['_error'] .= "Error fetching DeviceAtlas data from webservice.\n";
}
}

if(isset($da_results['mobileDevice']) && $da_results['mobileDevice']=='true') {
header('Location: ' . MOBILE_SITE);
die();

}
}
else {

//Set a cookie so we remember not to redirect again in the future
if (DA_USE_COOKIES && !isset($_COOKIE['Mobi_Mtld_DA_Prevent_Redirect'])) {
setcookie('Mobi_Mtld_DA_Prevent_Redirect', "true", time() + COOKIE_EXPIRY_TIME);

}
}
?>

Posted by lzbthmartini 3 years ago

This code works great, but recently I started seeing this message:

Notice: Undefined index: ALL_HTTP

for this statement:

if (strpos(strtolower($_SERVER['ALL_HTTP']),'operamini')>0) {
    $mobile_browser++;
}

I don't have an opera mini browser to test on. Is it something I should worry about?

thanks,
-Elizabeth

Posted by mksureshkumar 3 years ago

I have the same problem posted by LZBTHMARTINI... How to correct the problem

Posted by pilsnermonkey 3 years ago

Has anyone tried this script on a Flash page? New to mobi dev and Flash. Eek! Wondering if I might need to hit a php page first and redirect to Flash page for desktops/notebooks etc and then mobi domain for smart devices.

Posted by awm34x 3 years ago

Posted in error. Sorry!

Alan in Toledo

Posted by Boko 2 years ago

This is my version of the script, it is modified to detect the iPad as a mobile device and I corrected a bug that on mac and linux in my case redirected the Opera browser to the mobile site.
I tested the script on Mac OS X Lion, Windows XP, Ubuntu 11.10 with Firefox v9.0.1, Chrome v16 (Chromium v16 on Ubuntu), Opera v11.60 and Safari v5.1.2 web browsers, and also on iPod, iPhone, iPad and Blackberry.
If someone would like to optimize the script further please do share the result.

<?php
$mobile_browser = '0'; 
if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|pad|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
    $mobile_browser++;
} 
if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
    $mobile_browser++;
}     
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
$mobile_agents = array(
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
    'wapr','webc','winw','winw','xda ','xda-'); 
if (in_array($mobile_ua,$mobile_agents)) {
    $mobile_browser++;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'macintosh') > 0) {
    $mobile_browser = 0;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'linux') > 0) {
    $mobile_browser = 0;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
    $mobile_browser = 0;
}
if ($mobile_browser > 0) {
   header('Location: mobile/');
}
else {
   header('Location: main/');
} 
?>

Posted by JustinDoCanto 2 years ago

I modified your code to work a little more efficiently.

1. it's a function so it doesn't mess with other code/variables & can be easily included in a WordPress functions.php file or on any type of include, then referenced
2. I made the statements if/else if/else so that it doesnt go through all the code if it doesn't need to. makes for less time processing/loading.

the function is this:

function detectMobileDevice() {
	$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
	$mobile_agents = array(
		'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
		'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
		'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
		'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
		'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
		'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
		'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
		'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
		'wapr','webc','winw','winw','xda ','xda-');
	if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
		return true;
	}
	else if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
		return true;
	}   
	else if (in_array($mobile_ua,$mobile_agents)) {
		return true;
	}
	else if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) {
		return true;
	}
	else if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
		return true;
	}
	else {
		return false;
	}
}

and can be called like this...

if(detectMobileDevice()) {
   // if mobile device detected, do something
}
else {
   // otherwise do this...
}

if anybody needs mods to this or help, im not a normal mobiforge.com viewer so just email me at

Posted by taxishop 2 years ago

Hallo JustinDoCanto ...after the "windows" condition you must put "return false", otherwise you see also the desktop version in the mobile style...

anyway, thanks for posting, the function is easy to implement and works...at least on my devices. i did not test it on iphone...is there something else to add for iphone?

else if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
		return false;
	}