本帖最后由 刀心 于 2014-7-11 13:42 编辑
大神勿喷。
个人认为v9自带的手机门户功能不好用,而且用js判断体验也不佳。
首先感谢yidodo指点,才有此文。
思路如下:
1、自定义全局函数判断设备类型
2、pc访问为www域名,移动访问为wap域名
3、修改内容模型控制器,在选择模板的地方加上判断,如果是www域名则使用pc模板,如果是wap域名则使用移动模板。
好了就这样。
开始吧。判断设备类型的全局函数可以去网上找现成的。
- function isMobile(){
- $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
- $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';
- function CheckSubstrs($substrs,$text){
- foreach($substrs as $substr)
- if(false!==strpos($text,$substr)){
- return true;
- }
- return false;
- }
- $mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
- $mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');
-
- $found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) ||
- CheckSubstrs($mobile_token_list,$useragent);
-
- if ($found_mobile){
- return true;
- }else{
- return false;
- }
- }
复制代码
放到phpcms/libs/function/extention.function.php中。
来到/phpcms/modules/content
新建 MY_index.php文件,内容如下。
- <?php
- defined('IN_PHPCMS') or exit('No permission resources.');
- class MY_index extends index{
- private $db;
- function __construct() {
- $this->db = pc_base::load_model('content_model');
- $this->hosts = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
- parent::__construct();
- if(!strstr($this->hosts,'wap.')) $this->tomobile();
- }
- public function tomobile(){
- $host = str_replace('www.','wap.',$this->hosts);
- if(isMobile()){
- header("Location: $host");
- }
- }
- }
- ?>
复制代码
修改/phpcms/modules/content/index.php 文件
凡是include template出现的地方都做一下判断。
- if(strstr($this->hosts,'wap.')){
- include template('content','shouyewap',$default_style);
- }else{
- include template('content','index',$default_style);
- }
复制代码
当然要解析一个wap域名过来用。
同理其实可以扩展到其他的前台模板中。
仅适用于动态网站。生成静态的是无法判断的。
完毕。
|