Another Mobile Redirect Code Update
Posted by Quimby Melton on February 5, 2007
For an up-to-date list of posts/comments on this topic and a link to the most current version of the script(s), visit the MOBILE REDIRECT CODE DISCUSSION INDEX.
Thanks to a tip from Ralph, add this code to the very top of your .php homepage (above the page declaration: <!DOCTYPE … ) to redirect users to a mobile version of your website or use a PHP include like this:
<?php include (‘FULL PATH TO .PHP FILE’); ?>
I’ve also added the code to the updated version of the mobile redirect code bundle (mobile2.php). Download the bundle here.
Here’s the code:
<?php
$mobile = “YOUR MOBILE SITE HERE”;
$text = $_SERVER['HTTP_USER_AGENT'];
$var[0] = ‘Mozilla/4.’;
$var[1] = ‘Mozilla/3.0′;
$var[2] = ‘AvantGo’;
$var[3] = ‘ProxiNet’;
$var[4] = ‘Danger hiptop 1.0′;
$var[5] = ‘DoCoMo/’;
$var[6] = ‘Google CHTML Proxy/’;
$var[7] = ‘UP.Browser/’;
$var[8] = ‘SEMC-Browser/’;
$var[9] = ‘J-PHONE/’;
$var[10] = ‘PDXGW/’;
$var[11] = ‘ASTEL/’;
$var[12] = ‘Mozilla/1.22′;
$var[13] = ‘Handspring’;
$var[14] = ‘Windows CE’;
$var[15] = ‘PPC’;
$var[16] = ‘Mozilla/2.0′;
$var[17] = ‘Blazer/’;
$var[18] = ‘Palm’;
$var[19] = ‘WebPro/’;
$var[20] = ‘EPOC32-WTL/’;
$var[21] = ‘Tungsten’;
$var[22] = ‘Netfront/’;
$var[23] = ‘Mobile Content Viewer/’;
$var[24] = ‘PDA’;
$var[25] = ‘MMP/2.0′;
$var[26] = ‘Embedix/’;
$var[27] = ‘Qtopia/’;
$var[28] = ‘Xiino/’;
$var[29] = ‘BlackBerry’;
$var[30] = ‘Gecko/20031007′;
$var[31] = ‘MOT-’;
$var[32] = ‘UP.Link/’;
$var[33] = ‘Smartphone’;
$var[34] = ‘portalmmm/’;
$var[35] = ‘Nokia’;
$var[36] = ‘Symbian’;
$var[37] = ‘AppleWebKit/413′;
$var[38] = ‘UPG1 UP/’;
$var[39] = ‘RegKing’;
$var[40] = ‘STNC-WTL/’;
$var[41] = ‘J2ME’;
$var[42] = ‘Opera Mini/’;
$var[43] = ‘SEC-’;
$var[44] = ‘ReqwirelessWeb/’;
$var[45] = ‘AU-MIC/’;
$var[46] = ‘Sharp’;
$var[47] = ‘SIE-’;
$var[48] = ‘SonyEricsson’;
$var[49] = ‘Elaine/’;
$var[50] = ‘SAMSUNG-’;
$var[51] = ‘Panasonic’;
$var[52] = ‘Siemens’;
$var[53] = ‘Sony’;
$var[54] = ‘Verizon’;
$var[55] = ‘Cingular’;
$var[56] = ‘Sprint’;
$var[57] = ‘AT&T’;
$var[58] = ‘Nextel’;
$var[59] = ‘Pocket PC’;
$var[60] = ‘T-Mobile’;
$var[61] = ‘Orange’;
$var[62] = ‘Casio’;
$var[63] = ‘HTC’;
$var[64] = ‘Motorola’;
$var[65] = ‘Samsung’;
$var[66] = ‘NEC’;
$result = count($var);
for ($i=0;$i<$result;$i++)
{
$ausg = stristr($text, $var[$i]);
if(strlen($ausg)>0)
{
header(“location: $mobile”);
echo ‘<BR>Mobiles Geraet erkannt/mobile device detected<BR>’;
break;
}
}
?>
Many thanks, Ralph.
thanks for the help... said
….on detect mobile platform
scott said
I have a problem with the code working on ie7.
Whenever I attempt to access my site, a dialog box appears asking if I want to open or save index.xhtml (that is the file name of my mobile page). Would someone please explain what to do or how to avoid this from happening? Thanks.
studiohyperset said
Hi Scott.
I’m not sure what the dialog box is a function of, but it isn’t a result of the redirection script. I’ll feel pretty certain of that.
It’s possible that the file extension “.xhtml” is causing your browser, based on its configuration, to download the file rather than load it as a webpage.
I’d suggest changing the extension from “.xhtml” to “.htm” or “.html.” All virtually identical, I would be really shocked if you had the same result with a .html/.htm page.
Let me know how this works for you.
StPM said
In the first line of the code [ $mobile = “YOUR MOBILE SITE HERE”; ]: if your mobile site is a subdirectory of your main site, is there a particular convention for php purposes for designating your mobile site? That is, should it be: “http://mysite.com/mobile/” , or “http://mysite.com/mobile/index.html” , or something else?
Also, in the PHP Include snippet — where it asks for ‘FULL PATH TO .PHP FILE’ — if that php file is not in the root directory (for example, it’s in a subdirectory named “incl”) is there a particular convention for php purposes for designating the full path to that php file?
Please excuse the novice questions. And many thanks for this great bit of code.
StPM said
This code seems to work fine with the Opera Mini browser. With this code in place, when I access the site through the main (non-mobile) URL, the Opera Mini browser pulls up the mobile version of the site each time. And all the links on the main page of the mobile version to subpages of the mobile version work fine.
However, the coce doesn’t seem to work well with the built-in browser/MediaNet of my phone. It defaults to the non-mobile version every time, and only pulls up the mobile version upon reload. And, even when the main page of the mobile version appears upon reload, none of the links on the main page to other links on the mobile version of the site work (a Page Not Found error message.]
StPM said
Whoops! Cleared the cache, and now the built-in browser works fine, too.
Thanks for this code.
studiohyperset said
StPM,
I’ll answer your various inquiries as best I can and in one posting:
I’m glad clearing your cache worked for your phone. As you can see, the code is built around USER AGENT strings so if a particular USER AGENT isn’t listed, the mobile device likely won’t be redirected automatically. I’m always trying to add new USER AGENTS to the string; but with the panoply of devices and browsers, it’s going to be tough.
Optimally, what I’d like, is if I could find like an XML file that’s regularly updated with the USER AGENTS out there. The code could reference this XML file and be as accurate as it’s possible to be.
Interestingly enough, all this might also be moot in a few months. American mobile devices have always been behind the times compared to Euro phones and (especially) Asian devices. As a result, we’ve been sort of stumbling around here for the last few years, trying to make the web mobile friendly. However, devices like the iPhone have fully functioning browsers with Flash and other sorts of WAP functionality. We may sort of skip the need for mobile friendly (simple, text-heavy) sites and jump right into fully-functioning portable media devices.
At that point, this code will be a relic of the past; and we’ll all just surf the web with our mobile devices without need of a “mobile web.”
Finally, as for the path question you asked, go ahead and give the full path name to your mobile site. It can’t hurt; and depending on the server that processes your load request, it might mean the difference in the mobile page loading and nothing loading.
And no, there’s no special PHP convention for typing out the path. Just use the full URL path to the script and the mobile site.
Scott said
‘PPC’ seems to be tripping up Safari users on Power PC Macs and ‘Mozilla 4.’ was apparently tripping up a version of Internet Explorer on Windows. In case anyone else is having troubles.
ryan said
i am having trouble with blackberry’s working with the asp code. is there a way to target the phone by model number? does anyone know where to find the actual list of user agents that are used in there?
O. Quimby Melton said
Ryan,
I’m not personally sure about this, but I would think there are various ways to include Blackberry phones in either the list of vars or User Agents. Here’s a list of WAPs that might help: http://webcab.de/wapua.htm
For my part, I’m sort of over this code and am no longer managing or updating it. I think the iPhone renders it more or less obsolete since this device can display full versions of webpages (see http://studiohyperset.wordpress.com/2007/06/15/studio-hyperset-mobile-redirection-code-and-the-iphone/). We know other phones will and have followed so it’s just a matter of a few years before we in the US have, as they do in Europe and Japan, wonderful hand-held devices that allow us to access the full version of any webpage anytime.
Be well,
~Q.
sinej said
Hi!
1. Thank you for posting the code!
2. Code works on my BlackBerry but also automaticaly redirects if I am accesing from IE7 (on my desktope) while when accesing the page from Firefox it works fine.
3. If I change HTTP_USER_AGENT to REQUEST_URI IE7 shows OK but won’t redirect on BlackBerry.
What can be a problem?
Thnx,
O. Quimby Melton said
As this post suggests (http://studiohyperset.wordpress.com/2007/06/15/studio-hyperset-mobile-redirection-code-and-the-iphone/), I’m not really updating this script anymore. And since the scripts were all put together in advance of IE7, they could likely be outdated.
Think of these scripts as archives, then, that intrepid parties can use to build their own redirects codes. My advice is just play with what I have here until you get them to work. This is how I built them in the first place.
Be well.
Herman said
You should leave the mozzila 3.0 and 4.0 away.
phpshift said
The only thing I find helpful about this is the list of mobile agent data. The actual checking process seems rather odd. Wouldn’t it be simpler to just say:
foreach($vars as $v)
{
if(strpos($text,$v)>0)
header(“Location: $mobile”);
}
Also, there might be trouble with case sensitivity, so you might want to consider this:
$text=strtolower($text);
foreach($vars as $v)
{
if(strpos($text,strtolower($v))>0)
header(“Location: $mobile”);
}
O. Quimby Melton said
Good tip. Thanks so much. However, as I’ve noted, I’m not really updating this code any longer. The necessity of mobile redirection is fading fast with the advent of the iPhone age.
See this: http://studiohyperset.wordpress.com/2007/06/15/studio-hyperset-mobile-redirection-code-and-the-iphone/
help said
with that redirecting code, do i just put that on the head section, and what about calling it on the body section, do i need no to?
help please, is it possible also to change to a javascript code than using php?
thanks:)
Oliver Melton said
Funanani
This post should help you:
http://studiohyperset.wordpress.com/2006/11/12/mobile-redirect-update/
~Q.
Kevin Cuzner said
This particular script will redirect certain versions of internet explorer because they contain Mozilla/4.0 in their user agent string. To fix I added some code before the actual redirect which stops the page from redirecting if the string “MSIE” is found in the user agent.
maque.org.ua » Перенаправлення на звичайний та мобільний сайт said
[...] Detect Mobile Browsers 2.hyper/bo?l/e 3.Movable Type Forums 4. Створюємо мобільну версію блоґу з [...]