文章目录
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);
据此提示,修改并完成设置双模板的方法步骤:
- 按照路径
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);
并替换上述相同的代码,即可完成实现双端设置。
上述代码参考于:@http://blog.54zm.com/article/86
附:配置文件 · 一键下载:
相关文章推荐:
以上就是关于“ phpcms 怎样实现电脑,手机访问自动调用不同模板而URL地址不变 ” 的全部内容。