源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[PHP编程] thinkphp判断访客为手机端或PC端的方法

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2018-12-25 17:49:15 | 显示全部楼层 |阅读模式
这篇文章主要介绍了thinkphp判断访客为手机端或PC端的方法,通过获取用户$_SERVER['HTTP_USER_AGENT']信息后,再针对数组的遍历与匹配来判断访客类型,是非常实用的技巧,需要的朋友可以参考下

本文实例讲述了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框架程序设计有所帮助。

回复

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2022-9-10 09:29:58 | 显示全部楼层
感谢楼主分享
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

362

积分

中级会员

Rank: 3Rank: 3

积分
362
发表于 2022-11-25 09:48:12 | 显示全部楼层
看看看看看看看看看看看看看看看看看看看看看看看看看看看
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-1-23 11:11:11 | 显示全部楼层
借款金额看了就立刻
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

99

积分

注册会员

Rank: 2

积分
99
发表于 2023-10-20 12:58:25 | 显示全部楼层
哟哟哟哟哟以偶
回复 支持 反对

使用道具 举报

15

主题

2万

回帖

122

积分

注册会员

Rank: 2

积分
122
发表于 2024-3-1 00:27:42 | 显示全部楼层
灌灌灌灌水
回复 支持 反对

使用道具 举报

7

主题

2万

回帖

398

积分

中级会员

Rank: 3Rank: 3

积分
398
发表于 2024-3-3 12:32:28 | 显示全部楼层
天天源码论坛
回复 支持 反对

使用道具 举报

1

主题

1万

回帖

93

积分

注册会员

Rank: 2

积分
93
发表于 2024-3-9 12:15:13 | 显示全部楼层
问问问企鹅哇哇哇哇哇
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

69

积分

注册会员

Rank: 2

积分
69
发表于 2024-9-5 03:19:31 | 显示全部楼层
刷屏刷屏刷屏
回复 支持 反对

使用道具 举报

1

主题

1万

回帖

51

积分

注册会员

Rank: 2

积分
51
发表于 2024-9-9 07:33:00 | 显示全部楼层
hi哦和烦恼农家女
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-11-30 18:52 , Processed in 0.153751 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表