thinkphp判断访客为手机端或PC端的方法

 更新时间:2014年11月24日 16:20:41   转载 投稿:shichen2014  
这篇文章主要介绍了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框架程序设计有所帮助。

相关文章

  • Zend Framework教程之Zend_Helpers动作助手ViewRenderer用法详解

    Zend Framework教程之Zend_Helpers动作助手ViewRenderer用法详解

    这篇文章主要介绍了Zend Framework教程之Zend_Helpers动作助手ViewRenderer用法,结合实例形式较为详细的分析了Zend_Helpers动作助手ViewRenderer的功能、使用方法与相关注意事项,需要的朋友可以参考下
    2016-07-07
  • PHP 中常量的知识整理

    PHP 中常量的知识整理

    这篇文章主要介绍了PHP 中常量的知识整理的相关资料,需要的朋友可以参考下
    2017-04-04
  • php类

    php类

    php类...
    2006-11-11
  • tp5.1 框架路由操作-URL生成实例分析

    tp5.1 框架路由操作-URL生成实例分析

    这篇文章主要介绍了tp5.1 框架路由操作-URL生成,结合实例形式分析了tp5.1 框架针对路由操作URL生成的相关实现原理、操作技巧与注意事项,需要的朋友可以参考下
    2020-05-05
  • php 生成加密公钥加密私钥实例详解

    php 生成加密公钥加密私钥实例详解

    这篇文章主要介绍了php 生成加密公钥加密私钥实例详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • php header函数的常用http头设置

    php header函数的常用http头设置

    这篇文章主要介绍了php header函数的常用http头设置,本文直接给出代码实例,代码中包含详细注释,需要的朋友可以参考下
    2015-06-06
  • yiic命令时提示“php.exe”不是内部或外部命令的解决方法

    yiic命令时提示“php.exe”不是内部或外部命令的解决方法

    这篇文章主要介绍了yiic命令时提示“php.exe”不是内部或外部命令的解决方法,针对环境变量的配置与Yii框架的配置文件修改来解决这一问题,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-12-12
  • Laravel中encrypt和decrypt的实现方法

    Laravel中encrypt和decrypt的实现方法

    这篇文章主要给大家介绍了关于Laravel中encrypt和decrypt的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-09-09
  • PHP 只允许指定IP访问(允许*号通配符过滤IP)

    PHP 只允许指定IP访问(允许*号通配符过滤IP)

    今天有朋友问我如何只允许指定IP访问,默认禁止其它ip访问,脚本之家小编以前分享的是如何限制指定ip访问,这里正好分享下,需要的朋友可以参考下
    2014-07-07
  • 使用phpQuery获取数组的实例

    使用phpQuery获取数组的实例

    下面小编就为大家带来一篇使用phpQuery获取数组的实例.小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03

最新评论