自动识别WEB和WAP访客的源码
程序员文章站
2022-06-19 22:27:27
自动识别WEB和WAP访客的源码...
既然要利用已经存在的为html浏览器编写的代码,就需要知道请求是从html浏览器还是从wml浏览器过来的。同样地,如果想重新引导的html浏览器直接到相应的html文档上,wml浏览器到wml页面上,以下的php代码就可以做到这些。
<?
// because this script sends out http header information,
// the first characters in the file must be the <? php tag.
// relative url to your html file
$htmlredirect = "/html/my_htmlpage.html";
// absolute url to your wml file
$wmlredirect = "http://wap.mysite.com/wml/my_wmldeck.wml";
if(strpos(strtoupper($http_accept),"vnd.wap.wml") > 0)
{// check whether the browser/gateway says it accepts wml.
$br = "wml";
}
else {
$browser=substr(trim($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") {
// force the browser to load the wml file instead
header("302 moved temporarily");
header("location: ".$wmlredirect);
exit;
}
else {
// force the browser to load the html file instead
header("302 moved temporarily");
header("location: ".$htmlredirect);
exit;
}
?>
这个判断是在服务端完成的, php代码将首先查看网关是否接收text/vnd.wap.vml mime类型。如果不是,将检测前面的字符,查看是否为wml浏览器。如果不符合,那么就假设为html浏览器。如果有新的wml浏览器,那么id字符串也要增加。
这个代码基于robert whitinger(robert@wapsight.com)的代码,使用了don amaro(donamaro.concepcion@nl.unisys.com)提供的列表。
注意:由于只需要四个字符串就可以辨别,因此例如:"wapide-sdk/2.0;(r320s(arial))" 可以使用“wapi”来代替是可行的做法,也是足够的。
同样的功能也可以通过asp来解决。先判断请求的是“/index.wml” 或者 “/index.html” 和所需要的mime类型。另外以下的脚本辨别的方式和上面不一样。另外还需要网关告诉服务器它能接收的text/vnd.wap.wml mime类型。该例子如下所示:
<%
response.buffer = true
dim iswap
httpaccept = lcase(request.servervariables("http_accept"))
if instr(httpaccept,"wap") then
iswap=1
else response.redirect "/index.html" : 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%>
<?
// because this script sends out http header information,
// the first characters in the file must be the <? php tag.
// relative url to your html file
$htmlredirect = "/html/my_htmlpage.html";
// absolute url to your wml file
$wmlredirect = "http://wap.mysite.com/wml/my_wmldeck.wml";
if(strpos(strtoupper($http_accept),"vnd.wap.wml") > 0)
{// check whether the browser/gateway says it accepts wml.
$br = "wml";
}
else {
$browser=substr(trim($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") {
// force the browser to load the wml file instead
header("302 moved temporarily");
header("location: ".$wmlredirect);
exit;
}
else {
// force the browser to load the html file instead
header("302 moved temporarily");
header("location: ".$htmlredirect);
exit;
}
?>
这个判断是在服务端完成的, php代码将首先查看网关是否接收text/vnd.wap.vml mime类型。如果不是,将检测前面的字符,查看是否为wml浏览器。如果不符合,那么就假设为html浏览器。如果有新的wml浏览器,那么id字符串也要增加。
这个代码基于robert whitinger(robert@wapsight.com)的代码,使用了don amaro(donamaro.concepcion@nl.unisys.com)提供的列表。
注意:由于只需要四个字符串就可以辨别,因此例如:"wapide-sdk/2.0;(r320s(arial))" 可以使用“wapi”来代替是可行的做法,也是足够的。
同样的功能也可以通过asp来解决。先判断请求的是“/index.wml” 或者 “/index.html” 和所需要的mime类型。另外以下的脚本辨别的方式和上面不一样。另外还需要网关告诉服务器它能接收的text/vnd.wap.wml mime类型。该例子如下所示:
<%
response.buffer = true
dim iswap
httpaccept = lcase(request.servervariables("http_accept"))
if instr(httpaccept,"wap") then
iswap=1
else response.redirect "/index.html" : 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%>
上一篇: 利用字符串资源显示MessageBox
下一篇: 360网站点评认证上线了