像3G门户和QQ那样
你输入,如果是从电脑访问,他就跳转到电脑的网页
如果是用移动设备访问,就跳到wap的网页
有谁知道这个要怎弄?
=============================================
PHP思路和代码:
正巧我也在解决这个问题,和你分享一下我的发现.
网上现在有三种观点,一种是基于浏览器发送的User-Agent,但明显这种方法是行不通的,虽然有人列出了大多数的手机发送的User-Agent,但依然会有很多手机无法识别,甚至有些手机浏览器不发送User-Agent,而且也不能保证以后就不会出新牌子.
下面是一个php的例子,看过之后你就会知道有多么不可靠.
1.functionis_wap(){
2.$ua=strtolower($_SERVER['HTTP_USER_AGENT']);
3.$uachar="/(nokia|sony|ericsson|mot|samsung|sgh|lg|sie|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile)/i";
4.if(($ua==''||preg_match($uachar,$ua))&&!strpos(strtolower($_SERVER['REQUEST_URI']),'wap')){
5.returntrue;
6.}else{
7.returnfalse;
8.}
9.}
另外一种是判断HTTP_ACCEPT,这种应该相对可靠一点,不过HTTP_ACCEPT是非常复杂的,如果一般的不支持html的浏览器还好说,你只要判断浏览器支持wml并且不支持html就可以,但如果浏览器同时支持wml和html那就难办了,低端手机可能会将wml放到html的前面,但是很多高端手机或者智能手机对html的支持很好,所以html也会出现在wml前面.
这是黑莓手机发送的HTTP_ACCEPT
application/vnd.rim.html,
text/html,
application/vnd.wap.xhtml+xml,
text/vnd.sun.j2me.app-descriptor,
image/vnd.rim.png,image/jpeg,
application/x-vnd.rim.pme.b,
application/vnd.rim.ucs,
image/gif;anim=1,
application/vnd.rim.jscriptc;v=0-8-8,
application/x-javascript,
application/vnd.rim.css;v=1,
text/css;media=handheld,
application/vnd.wap.wmlc;q=0.9,
application/vnd.wap.wmlscriptc;q=0.7,
text/vnd.wap.wml;q=0.7,
**;q=0.8
同样,给你个php的例子.
if(isset($_SERVER['HTTP_ACCEPT'])&&
(strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml')!==FALSE)
&&(strpos($_SERVER['HTTP_ACCEPT'],'text/html')===FALSE||
(strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml')<
strpos($_SERVER['HTTP_ACCEPT'],'text/html'))
)){//手机访问
readfile('index.wml');
}elsereadfile('index.htm');
原理:如果浏览器支持WML、而且[不支持HTML]或[WML优先于HTML],则判定为手机。但实际上这个原理是不完全正确的.
还有一种是说判断网关或者IP什么的,但我个人认为这个很不可靠,手机的联网方式很多,比如我开发的时候就是用模拟器的.这个应该不用什么例子了,REMOTE_ADDR就足够了.
以上是我今天上午的工作成果,但实际上还是没有找到完美的解决方案,先写这么多,有问题请补充或者发送消息给我,如果你有更好的办法希望可以分享给我,谢谢.
补充:刚刚吃饭的时候想如果浏览器支持js判断屏幕尺寸也是个不错的方法.
=========================================
ASP的方案和代码范例:
首先是通过HTTP_ACCEPT简单判断是否支持WAP的,这个逻辑太简单,也不准确:
ifInStr(LCase(Request.ServerVariables("HTTP_ACCEPT")),"text/vnd.wap.wml")>0then
response.redirect/wap'如果是手机访问则跳转WAP页面
response.end
else
response.redirect'如果电脑访问跳转到首页
response.endendif
%>
通过HTTP_USER_AGENT判断用户是从手机上访问,还是电脑IE上访问。asp代码片段:主要使用了正则匹配手机环境,大家可以补充手机平台。
setregex=newregexpregex.ignorecase=trueregex.global=trueregex.pattern="mini9.5|vx1000|lge|m800|e860|u940|ux840|compal|wireless|mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|flyv71|s302|-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|n120|mtk|c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|mw200|brew|d88|htc\/|htc_touch|355x|m50|km100|d736|p-9521|telco|sl74|ktouch|m4u\/|me702|8325rc|kddi|phone|lg|sonyericsson|samsung|240x|x320vx10|nokia|sonycmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|psp|treo|iris|3g_t|windowsce|operamobi|windowsce;smartphone;|windowsce;iemobile|ipod|iphone|android|operamini|blackberry|palmos|palm|hiptop|avantgo|fennec|plucker|xiino|blazer|elaine|iris|3g_t|windowsce|operamobi|windowsce;smartphone;|windowsce;iemobile"agent=request.ServerVariables("HTTP_USER_AGENT")&""ifagent<>""thenifnotregex.test(agent)then'不是从手机来的访问,跳转到你指定网址response.redirect("")endifendif
%>
再给个例子给你:这里判断不是手机访问就跳转
dimtempUrl,browsers
browsers=Lcase(Left(Request.ServerVariables("HTTP_USER_AGENT"),4))'判断手机访问
ifbrowsers="oper"orbrowsers="winw"orbrowsers="wapi"orbrowsers="mc21"orbrowsers="up.b"orbrowsers="upg1"orbrowsers="upsi"orbrowsers="qwap"orbrowsers="jigs"orbrowsers="java"orbrowsers="alca"orbrowsers="wapj"orbrowsers="fetc"orbrowsers="r380"orbrowsers="winw"orbrowsers="mozi"orbrowsers="mozi"orbrowsers="m3ga"then
response.redirect""
endif
tempUrl=request.servervariables("HTTP_HOST")
iftempUrl<>""then
response.redirect""
endif