With the blaze of the iPhone and other similarly enabled "web" phones, companies such as Bank of America, ABC News, Flickr and Orbitz.com have made an extra effort to make their exceptional websites available across these smaller platforms.
These sites present their content in a slightly different way. They can present more "design" than previous "WAP" enabled phones, but slight less "action" than our normal heavy-hitter browsers can process.
With the improvement of these new mobile web browsers, mobile web content only needs to meet the basic HTML / Javascript standards. These slight differences allow for greater creativity and less overhead in translation costs. Most developers can easily implement a switch that can present a downgraded version of the original website. The method in which they do this can vary based on server configurations. Although there are several methods readily available through a google search, the code below, created by Andy Moore and updated by dev.mobi, is a great light PHP script.
The "proper" way to do this is to use a full device database such as WURFL (and we have an article on how to do this) - dev.MOBI
If you can't implement WURL (or similar) then maybe one or both scripts below can help redirect you visitors to your new mobile web site.
Script ONE (includes header and agents test)
<?php
$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']),'windows')>0) {
$mobile_browser=0;
}
if($mobile_browser>0){
// do something
} else {
// do something else
}
?>
Script TWO (includes a simple header detection test)
<?php
/*
SHARED LICENSE - VISIT ANDYMOORE.info
*/
function detect_mobile_device(){
// check if the user agent value claims to be windows but not windows mobile
if(stristr($_SERVER['HTTP_USER_AGENT'],'windows')&&!stristr($_SERVER['HTTP_USER_AGENT'],'windows ce')){
return false;
}
// check if the user agent gives away any tell tale signs it's a mobile browser
if(eregi('up.browser|up.link|windows ce|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp',$_SERVER['HTTP_USER_AGENT'])){
return true;
}
// check the http accept header to see if wap.wml or wap.xhtml support is claimed
if(stristr($_SERVER['HTTP_ACCEPT'],'text/vnd.wap.wml')||stristr($_SERVER['HTTP_ACCEPT'],'application/vnd.wap.xhtml+xml')){
return true;
}
// check if there are any tell tales signs it's a mobile device from the _server headers
if(isset($_SERVER['HTTP_X_WAP_PROFILE'])||isset($_SERVER['HTTP_PROFILE'])||isset($_SERVER['X-OperaMini-Features'])||isset($_SERVER['UA-pixels'])){
return true;
}
// build an array with the first four characters from the most common mobile user agents
$a = array('acs-','alav','alca','amoi','audi','aste','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','opwv','palm','pana','pant','pdxg','phil','play','pluc','port','prox','qtek','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','w3c ','wap-','wapa','wapi','wapp','wapr','webc','winw','winw','xda','xda-');
// check if the first four characters of the current user agent are set as a key in the array
if(isset($a[substr($_SERVER['HTTP_USER_AGENT'],0,4)])){
return true;
}
}
// example 1 - detect and redirect mobile browsers
if(detect_mobile_device()){
header('Location: http://andymoore.mobi/');
exit;
}
// example 2 - detect and redirect desktop browsers
if(!detect_mobile_device()){
header('Location: http://andymoore.info/');
exit;
}
?>


Check a java version of the php detection code:
http://code.google.com/p/mobiledevicedetector/
How do I implement a code for HTML website?? Php is easy, just copy paste into index.php. What about HTML?
Please, help!
Niko, I’m not exactly sure on what you mean. Are you trying to use the above code in your site? If you are, you need php to detect. The code above is a basic switch…
You can place this in a file that’s included in all pages at top. If a mobile device is detected then it will redirect to the page you decide to show mobiles. If not, you can allow it to continue normally.
Did I clear that up a little better? Take a look at the test pages. Click Here - you can see how the page changes if you use a mobile browser.
Yes, thank you. Actually, I wonder which code to use for HTML language. Your code above works perfectly on php language. I have a site written in HTML so therefore I would like to know a proper code for HTML. Or it is the same?
Thank’s for help in advance!
Well actually this code is for php pages only. The basic schematics look like this:
- index.php page with php code redirects to any page (php or html) ->
[php code] —|
|— mobile (this could be html)
|
|— not mobile (this could be html)
Maybe do you know where could I get a code for HTML? For example, I have HTML site and I would like to install detection code in it in order to detect a device.
I have tested your code for php and it works perfectly! But I can’t implement this into html. So I’m a little worried because it seems that no one can help me.
There’s no way to do that conditional redirection with HTML. JavaScript might help you with redirecting based on user agent. This might be tricky because some mobile devices don’t handle JavaScript properly.
However, if you have access to PHP on your server, I recommend that you use this since the “work” is done on the server and doesn’t rely on the browser or operation system (example: WinCE or Blackberry)
Thanks for the great explanation. I am new to PHP. I do use formmail.php to process a form from a web page, but I am having trouble with the above codes.
First, I am unsure of how the code should look. Basically I want the code to reside in my index.html file if possible & just redirect any mobile browsers to http://mywebsite/m. If it is not mobile, I would like the code to just continue on & display the page as normal.
Would the following lines be correct:
52. // example 1 - detect and redirect mobile browsers
53.
54. if(detect_mobile_device()){
55.
56. header(’Location: http://nyfootexpert.com/m/index.html/‘);
57.
58. exit;
59.
60. }
61.
73.
74. ?>
I tested the script and it works great, thank you. And a BIG thank you for making it so clear to us Graphic Designer who dabble in the web because out clients require us to! :)
Question: Is it possible to have a script that detects the mobile device and then sends the visitor to a page that fits the mobile device size? I notice that on different devices, the page is not always clean and clear. Some are narrower, etc.
Is this a good idea or am I making it more difficult than I need to?
Thank you,
S Parrott
OneWay Advertising
These scripts work great for detecting between desktop and mobile (.mobi), but is there a way to detect whether a user’s mobile is only able to display .wml? I unfortunately have to cater for the older phones in my application as well.