温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

thinkphp如何判断访客为手机端或PC端

发布时间:2021-03-06 15:04:06 来源:亿速云 阅读:387 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关thinkphp如何判断访客为手机端或PC端,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

一、问题:

近日准备给自己的网站做一个小升级,让用户在手机二维码扫描的时候显示适合手机端来展示的模版【我用的是ThinkPHP3.0】,代码是参考别人的

二、实现方法:

这里先说下大概的一个思路 简单两步:

统版本号和浏览器以及它的版本号(贴出的代码中数组中只有移动端信息,因此后面只需判断是否为数组中的某一个值即可).

根据数组中的值来判断访客是否为手机、pad、之类的移动端,如果是的话就规定项目路径、名称为你的手机端模版如

复制代码 代码如下:

define('APP_NAME','mobi');

define('APP_PATH','./mobi/');

下面代码的话我尽量加上注释:

//判断是否属手机  
//代码看上去很多,其实就是数组里面显得多而乱,不要被表面现象所吓倒哦!  
 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)) { //stristr 查找访客端信息是否在上述数组中,不存在即为PC端。  
            $is_mobile = true;  
            break;  
        }  
    }  
    return $is_mobile;  
}  
define('THINK_PATH','./CORE/');  
if(is_mobile()){ //跳转至wap分组  
 echo '您是手机端访问的,已跳转到手机端';  
     define('APP_NAME','mobi');  
    define('APP_PATH','./mobi/');  
}else{  
 echo '你是PC端访问的';  
     define('APP_NAME','Home');  
    define('APP_PATH','./Home/');           
}  
 define('APP_DEBUG', false);  
require THINK_PATH.'core.php';

关于“thinkphp如何判断访客为手机端或PC端”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI