Since it’s very practical to re-use already existing code that you have written for HTML browsers, you need a way to tell HTML browsers from WML browsers. Also, you might want to redirect HTML browsers to a directory with HTML documents, and WML browsers to WML decks. The PHP code below does just this. The reason the script is written to work this way is that it’s easier to understand the code.
The popular Apache web server has a module called mod_rewrite which can also be used for this purpose. See Philip J. Mikal's ''mod_rewrite'' example available at his site.
This has to be done on the server side, and the following PHP code will look first attempt to discover if the WAP gateway being used can accept the text/vnd.wap.vml MIME type. If not, it will check the first four characters in the ID string to determine if it’s a WML browser. If there’s no match, it’s assumed that it’s an HTML browser. As new WML browsers come along, their ID strings should be added to the list.
The code is based on Robert Whitinger’s (robert@wapsight.com) code submitted to the PHP mailing list, with several additions for browser ID string from Don Amaro’s (donamaro.concepcion@nl.unisys.com) log files
Note that the strings below are only the four first characters of the ID strings. The whole string is for instance “WapIDE-SDK/2.0;(R320s(Arial))” for the Ericsson WapIDE 2.0. Basically, we’re just after enough to make it unambiguous.
<? // Because this script sends out HTTP header information, the first characters in the file must be the <? PHP tag. $htmlredirect = "/html/my_htmlpage.asp"; // relative URL to your HTML file $wmlredirect = "http://wap.mysite.com/wml/my_wmldeck.wml"; // ABSOLUTE URL to your WML file if(strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0) { // Check whether the browser/gateway says it accepts WML. $br = "WML"; } else { $browser=substr(trim($_SERVER['HTTP_USER_AGENT']),0,4); if($browser=="Noki" || // Nokia phones and emulators $browser=="Eric" || // Ericsson WAP phones and emulators $browser=="WapI" || // Ericsson WapIDE 2.0 $browser=="MC21" || // Ericsson MC218 $browser=="AUR " || // Ericsson R320 $browser=="R380" || // Ericsson R380 $browser=="UP.B" || // UP.Browser $browser=="WinW" || // WinWAP browser $browser=="UPG1" || // UP.SDK 4.0 $browser=="upsi" || // another kind of UP.Browser ?? $browser=="QWAP" || // unknown QWAPPER browser $browser=="Jigs" || // unknown JigSaw browser $browser=="Java" || // unknown Java based browser $browser=="Alca" || // unknown Alcatel-BE3 browser (UP based?) $browser=="MITS" || // unknown Mitsubishi browser $browser=="MOT-" || // unknown browser (UP based?) $browser=="My S" || // unknown Ericsson devkit browser ? $browser=="WAPJ" || // Virtual WAPJAG www.wapjag.de $browser=="fetc" || // fetchpage.cgi Perl script from www.wapcab.de $browser=="ALAV" || // yet another unknown UP based browser ? $browser=="Wapa") // another unknown browser (Web based "Wapalyzer"?) { $br = "WML"; } else { $br = "HTML"; } } if($br == "WML") { header("302 Moved Temporarily"); // Force the browser to load the WML file instead header("Location: ".$wmlredirect); exit; } else { header("302 Moved Temporarily"); // Force the browser to load the HTML file instead header("Location: ".$htmlredirect); exit; } ?>
The same kind of functionality can be done in ASP. Robin van Emden (rave@dataweb.nl) has done the following script which takes the browser to either “/index.wml” or “/index.asp” depending on what sort of MIME types the browser (or gateway) accepts. Note that the following script is not *identical* in functionality to the script above. Also note that this requires the gateway to tell the web server that it really accepts the text/vnd.wap.wml MIME type, while the script above first checks if the browser can accept the MIME type, then checks the identification of the browser.
<% Response.Buffer = TRUE Dim IsWap httpAccept = LCase(Request.ServerVariables("HTTP_ACCEPT")) if Instr(httpAccept,"wap") then IsWap=1 Else Response.Redirect "/index.asp" : Response.Flush : Response.End End if %><%Response.ContentType = "text/vnd.wap.wml"%><?xml version="1.0"?><%Response.Flush%> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"> <wml> <card id="redirect"> <onevent type="onenterforward"> <go href="/index.wml"/> </onevent> <p> <a href="/index.wml">enter</a> </p> </card> </wml> <%Response.Flush:Response.End%>
Lately (December 2006), Luca Passani has released the Switcher, http://www.passani.it/switcher/, a Java servlet with a high-degree of logic that will tell a desktop browser from a mobile browser *programmatically*. While the Switcher is not open source, the source code can be purchased for a fee.
You may also choose to use WURFL to distinguish mobile browsers from desktop computers. You should apply the web browsers patch and then use the appropriate capabilities. WURFL provides you information about 2 important matters, if it’s a mobile devices and if the browser is supposed to support full HTML (frames, pop-up windows, javascript, etc). One of the two capabilities or a combination should provide you all the needed information depending on your site and target devices (mobile phones, smartphones, PDA‘s, other).
WURFL capability: is_wireless_device |
|---|
WURFL capability: device_claims_web_support |
|---|