phpcms 实现PC端、手机端的双模版

phpcms 怎样实现电脑,手机访问自动调用不同模板而URL地址不变? 注意: 替换的代码如下,几处位置替换的代码都一样。 替换的文件路径如下: www\phpcms\modules\content\index.php 方...

phpcms 怎样实现电脑,手机访问自动调用不同模板而URL地址不变?


注意: 替换的代码如下,几处位置替换的代码都一样。


替换的文件路径如下:

www\phpcms\modules\content\index.php


方法一:

准备替换为代码:

/*判断客户端*/
if(substr($_SERVER['SERVER_NAME'], 0,1) == 'm'){
include template('content_m','index',$default_style);
}elseif(stristr($_SERVER['HTTP_VIA'],"wap")){// 先检查是否为wap代理,准确度高
    include template('content_m','index',$default_style);
}elseif(strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0){// 检查浏览器是否接受 WML.
    include template('content_m','index',$default_style);
}elseif(preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])){//检查USER_AGENT
    include template('content_m','index',$default_style);  
}else{
    include template('content','index',$default_style);
}


修改index.php文件,需要替换的代码的几处位置提示:

第一处: 第31行的 include template('content','index',$default_style);

第二处: 第203行 include template('content',$template);

第三处: 第265行 include template('content',$template);

第四处: 第278行 include template('content',$template);


据此提示,修改并完成设置双模板的方法步骤:


按照路径 www\phpcms\template,在 template文件夹下面 新建 content_m文件夹;

在 content_m下面放入事先写好的 html 手机端模板页面;

复制本文的替换代码,并找到目标项目的指定路径index.php文件,根据上述提供的几处位置,进行替换修改,【路径如下】:

www\phpcms\modules\content\index.php

直接覆盖替换原有的 index.php文件即可;

已经完成手机模板的相关设置【修改后的代码截图,此处省略,不再展示】;

方法二:(修改两处)

打开/phpcms/libs/functions/global.func.php


新增如下代码:


/**
 * Function isMobile
  * @param $n 判断手机版
 */
 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/modules/content/index.php

大约在31行处,找到 include template('content','index',$default_style); 并修改为:


if(isMobile()){
include template('content_m','index',$default_style);
}else{
include template('content','index',$default_style);
}


然后在203、265、278行处,分别找到 include template('content',$template);

并替换上述相同的代码,即可完成实现双端设置。


你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
admin
admin

651 篇文章

作家榜 »

  1. admin 651 文章
  2. 粪斗 185 文章
  3. 王凯 92 文章
  4. 廖雪 78 文章
  5. 牟雪峰 12 文章
  6. 李沁雪 9 文章
  7. 全易 2 文章
  8. Stevengring 0 文章