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



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

说到此处,不得不联系到 phpcms中关于“ 设备类型的检测与判断 ”的设置办法

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

替换的文件路径如下:

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);

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

  1. 按照路径 www\phpcms\template,在 template文件夹下面 新建 content_m文件夹;
  2. content_m下面放入事先写好的 html 手机端模板页面;
  3. 复制本文的替换代码,并找到目标项目的指定路径index.php文件,根据上述提供的几处位置,进行替换修改,【路径如下】:
    www\phpcms\modules\content\index.php
  4. 直接覆盖替换原有的 index.php文件即可;
  5. 已经完成手机模板的相关设置【修改后的代码截图,此处省略,不再展示】;


方法二:(修改两处)

  1. 打开/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;  
     	}  
     } 
    
  2. 打开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);
    并替换上述相同的代码,即可完成实现双端设置。

上述代码参考于:@http://blog.54zm.com/article/86

附:配置文件 · 一键下载:


相关文章推荐:


以上就是关于“ phpcms 怎样实现电脑,手机访问自动调用不同模板而URL地址不变 ” 的全部内容。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值