.htaccess与php判断手机/电脑访问手机页面

首页页面为网站更目录m下,后门参数和pc页面参数一样的话就可以这样,否则可能需要修改一下。
手机访问电脑页面时跳转:

 代码如下 复制代码
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /m
RewriteCond %{HTTP_USER_AGENT} “android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos” [NC]
RewriteRule ^(.*)$ /m/$1 [L,R=302] [L,R=302]
</IfModule>

?
电脑访问手机时跳转:

 代码如下 复制代码

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "!(android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos)" [NC]
RewriteRule ^(.*)$ /$1 [L,R=302]

</IfModule>

最后得说一下,因为RewriteRule ^(.*)$ /m/$1 [L,R=302] [L,R=302]的加入,如果手机页面中要访问非手机页面中的资源时,得用正则排除指定文件,如: RewriteRule ^(.*?(?<!jpg)$) /m/$1 [L,R=302],就不会造成手机访问非手机页面中jpg图片时出现无法访问的问题了。

如果你不能使用.htaccess文件我们可直接在php中加入下面代码

 代码如下 复制代码

//判断是否属手机
function is_mobile() {
 $user_agent = $_SERVER['HTTP_USER_AGENT'];
 $mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
 $is_mobile = false;
 foreach ($mobile_agents as $device) {
  if (stristr($user_agent, $device)) {
   $is_mobile = true;
   break;
  }
 }
 return $is_mobile;
}

第二步:if 语句

 代码如下 复制代码
if( is_mobile() ){
 就跳到手机页面哦
}
else
{
中到PC页面
}
时间: 2024-09-28 01:54:54

.htaccess与php判断手机/电脑访问手机页面的相关文章

项目从tomcat6移到tomcat7上,大部分电脑访问正常,但有几台电脑访问时页面空白

问题描述 项目从tomcat6移到tomcat7上,大部分电脑访问正常,但有几台电脑访问时页面空白那几台电脑访问tomcat首页时正常,所以网络没问题求问大牛有没能解决的办法啊?? 解决方案 解决方案二:补充楼上,后台没有报错解决方案三:这个真心不好说,这几台电脑,先排除本地浏览器兼容,缓存之类的问题再看看解决方案四:引用2楼shine333的回复: 这个真心不好说,这几台电脑,先排除本地浏览器兼容,缓存之类的问题再看看 跟浏览器兼容应该没关系到,因为用了很多不同的浏览器是试过了,包括火狐和23

php如何判断是手机访问还是电脑访问

要求网站要判断是手机访问还是电脑访问,然后显示不同的信息 <?php function isMobile(){ $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:''; function CheckSubstrs($s

PHP如何识别是电脑或手机访问网站

现在通过手机访问网站越来越流行了,如果我们希望统计一下网站通过pc,手机移动端的各自访问量的情况,或者需要为手机移动端做一些特别的处理的话,那么我们就需要对访问网站的用户的客户端做一下鉴别了,下面这个实例就是通过php识别用户是电脑还是手机访问网站的方法. <?php function isMobile(){ $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; $useragent_c

php是如何判定手机、电脑访问自动跳转头文件的

php判定手机电脑访问自动跳转头文件代码如下: <?php  function is_mobile_request()   {    $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';    $mobile_browser = '0';    if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|

如果判断用户第一次访问页面,并在这个页面退出时给提示!

问题描述 如果判断用户第一次访问页面,并在这个页面退出时给提示! 解决方案 解决方案二:用个表来记录解决方案三:如果判断用户第一次访问页面,并在这个页面退出时给提示!表達不是很清楚?解决方案四:楼上的具体说说,如果判断第一次访问,如果判断退出?解决方案五:用Session解决方案六:1.如何判断用户第一次访问该页面2.如何判断该用户第一次访问后退出,退出的时候给友好提示!比如推荐收藏类解决方案七:当用户访问页面时.与表的数据匹配.如果有记录那么就不是第一次访问.解决方案八:晕,一天有几十万的访问

js判断是手机访问还是电脑访问,进行自动跳转

 <script type="text/javascript"> <!-- //平台.设备和操作系统 var system ={ win : false, mac : false, xll : false }; //检测平台 var p = navigator.platform; alert(p); system.win = p.indexOf("Win") == 0; system.mac = p.indexOf("Mac")

php判断手机访问还是电脑访问示例分享

 这篇文章主要介绍了网站判断是手机访问还是电脑访问,然后显示不同的信息,大家参考使用吧 代码如下: <?php function isMobile(){    $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';    $useragent_commentsblock=preg_match('|(.*?)|',$useragent,$matches)>0?$matches[0]:'';

怎么判断访问端是电脑还是手机?

  网上现在有三种观点, 一种是基于浏览器发送的 User-Agent, 但明显这种方法是行不通的, 虽然有人列出了大多数的手机发送的User-Agent, 但依然会有很多手机无法识别, 甚至有些手机浏览器不发送User-Agent, 而且也不能保证以后就不会出新牌子. 下面是一个php的例子, 看过之后你就会知道有多么不可靠. 1. function is_wap(){ 2. $ua = strtolower($_SERVER['HTTP_USER_AGENT']); 3. $uachar =

asp下通过HTTP_USER_AGENT判断用户是从手机上访问,还是电脑IE上访问_应用技巧

通过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|