android连接php还是asp,ASP或PHP如何判断是从电脑还是手机客户端访问页面的思路...

像3G门户和QQ那样

你输入3g.cn,如果是从电脑访问,他就跳转到电脑的网页

如果是用移动设备访问,就跳到wap的网页

有谁知道这个要怎弄?

=============================================

PHP思路和代码:

正巧我也在解决这个问题, 和你分享一下我的发现.

网上现在有三种观点, 一种是基于浏览器发送的 User-Agent, 但明显这种方法是行不通的, 虽然有人列出了大多数的手机发送的User-Agent, 但依然会有很多手机无法识别, 甚至有些手机浏览器不发送User-Agent, 而且也不能保证以后就不会出新牌子.

下面是一个php的例子, 看过之后你就会知道有多么不可靠.

1. function is_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. return true;

6. }else{

7. return false;

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');

} else readfile('index.htm');

原理:如果浏览器支持WML、而且[不支持HTML]或[WML优先于HTML],则判定为手机。但实际上这个原理是不完全正确的.

还有一种是说判断网关或者IP什么的, 但我个人认为这个很不可靠, 手机的联网方式很多, 比如我开发的时候就是用模拟器的.这个应该不用什么例子了, REMOTE_ADDR 就足够了.

以上是我今天上午的工作成果, 但实际上还是没有找到完美的解决方案, 先写这么多, 有问题请补充或者发送消息给我, 如果你有更好的办法希望可以分享给我, 谢谢.

补充: 刚刚吃饭的时候想如果浏览器支持js判断屏幕尺寸也是个不错的方法.

=========================================

ASP的方案和代码范例:

首先是通过HTTP_ACCEPT简单判断是否支持WAP的,这个逻辑太简单,也不准确:

if InStr(LCase(Request.ServerVariables("HTTP_ACCEPT")),"text/vnd.wap.wml")>0 then

response.redirect http://www.yici.net/wap '如果是手机访问则跳转WAP页面

response.end

else

response.redirect http://www.yici.net '如果电脑访问跳转到首页

response.end end if

%>

通过HTTP_USER_AGENT判断用户是从手机上访问,还是电脑IE上访问。 asp代码片段:主要使用了正则匹配手机环境,大家可以补充手机平台。

set regex = new regexp regex.ignorecase = true regex.global = true regex.pattern = "mini 9.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|fly v71|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|sony cmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|psp|treo|iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile|ipod|iphone|android|opera mini|blackberry|palm os|palm|hiptop|avantgo|fennec|plucker|xiino|blazer|elaine|iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile" agent = request.ServerVariables("HTTP_USER_AGENT") & "" if agent <> "" then if not regex.test(agent) then '不是从手机来的访问,跳转到你指定网址 response.redirect("http://www.yici.net") end if end if

%>

再给个例子给你:这里判断不是手机访问就跳转

dim tempUrl,browsers

browsers=Lcase(Left(Request.ServerVariables("HTTP_USER_AGENT"),4)) '判断手机访问

if browsers="oper" or browsers="winw" or browsers="wapi" or browsers="mc21" or browsers="up.b" or browsers="upg1" or browsers="upsi" or browsers="qwap" or browsers="jigs" or browsers="java" or browsers="alca" or browsers="wapj" or browsers="fetc" or browsers="r380" or browsers="winw" or browsers="mozi" or browsers="mozi" or browsers="m3ga" then

response.redirect "http://www.baidu.com"

end if

tempUrl=request.servervariables("HTTP_HOST")

if tempUrl <>"wap.gyshty.cn" then

response.redirect "http://www.baidu.com"

end if

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值