phpcms实现电脑端和移动端不同模板
文章描述:
phpcms如何添加手机模板,当用户用电脑访问是显示PC模板,用手机访问显示手机模板
1、打开phpcms/libs/functions/global.func.php,在底部新增一个判断是否移动设备的方法,这个方法我们命名为isMobile()
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;
}
}
或者
function isMobile() {
// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
return true;
}
// 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
if (isset($_SERVER['HTTP_VIA'])) {
// 找不到为flase,否则为true
return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
}
// 脑残法,判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile','MicroMessenger');
// 从HTTP_USER_AGENT中查找手机浏览器的关键字
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
return true;
}
}
// 协议法,因为有可能不准确,放到最后判断
if (isset ($_SERVER['HTTP_ACCEPT'])) {
// 如果只支持wml并且不支持html那一定是移动设备
// 如果支持wml和html但是wml在html之前则是移动设备
if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
return true;
}
}
return false;
}
2、判断访问的是首页、栏目页、列表页、内容页和单页,根据不同加载不同的模板页面,打开phpcms/modules/content/index.php
首页
找到init()方法把
include template('content','index',$default_style);
替换
if(isMobile()){
include template('mobile','index',$default_style);
}else{
include template('content','index',$default_style);
}
内容页
找到show()方法把
include template('content',$template);
替换
if(isMobile()){
include template('mobile',$template);
}else{
include template('content',$template);
}
列表页
找到list()
include template('content',$template);
替换
if(isMobile()){
include template('mobile',$template);
}else{
include template('content',$template);
}
单页面
include template('content',$template);
替换
if(isMobile()){
include template('mobile',$template);
}else{
include template('content',$template);
}
3、在phpcms/templates/default目录下面新建一个mobile文件夹,在mobile里面新建文件
index.html首页
category.html栏目页
list.html列表页
show.html内容页
header.html头部
footer.html尾部
4、重新伪静态规则
在网站设置了生成静态文件的时候,默认的伪静态规则是识别不了访问的,这时需要修改伪静态规则,我们按照以下规则来修改。
location / {
index index.php index.html index.htm index.php;
#autoindex on;
rewrite ^/content-([0-9]+)-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3 last;
rewrite ^/list-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=lists&catid=$1&page=$2 last;
rewrite ^/show-([0-9]+)-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3 last;
rewrite ^/tag/(.*)_([0-9]+) /index.php?m=content&c=tag&a=lists&tag=$1&page=$2 last;
rewrite ^/tag/(.*) /index.php?m=content&c=tag&a=lists&tag=$1&page=$2 last;
#loan news
rewrite ^/news/index.html /index.php?m=content&c=index&a=lists&catid=1 last;
rewrite ^/news/index_([0-9]+).html /index.php?m=content&c=index&a=lists&catid=1&page=$1 last;
rewrite ^/news/([0-9]+).html /index.php?m=content&c=index&a=show&catid=1&id=$1 last;
rewrite ^/news/([0-9]+)_([0-9]+).html /index.php?m=content&c=index&a=show&catid=1&id=$1&page=$2 last;
rewrite ^/news/ /index.php?m=content&c=index&a=lists&catid=1 last;
rewrite ^/news /index.php?m=content&c=index&a=lists&catid=1 last;
#loan goods /daikuan/
rewrite ^/goods/index.html /index.php?m=content&c=index&a=lists&catid=2 last;
rewrite ^/goods/index_([0-9]+).html /index.php?m=content&c=index&a=lists&catid=2&page=$1 last;
#loan fangchan /fcdk/index.html
rewrite ^/fcdk/index.html /index.php?m=content&c=index&a=lists&catid=3 last;
rewrite ^/fcdk/index_([0-9]+).html /index.php?m=content&c=index&a=lists&catid=3&page=$1 last;
rewrite ^/fcdk/index_/([0-9]+).html /index.php?m=content&c=index&a=show&catid=3&id=$1 last;
rewrite ^/fcdk/index_/([0-9]+)_([0-9]+).html /index.php?m=content&c=index&a=show&catid=3&id=$1&page=$2 last;
#loan xinyong /xydk/index.html
rewrite ^/xydk/index.html /index.php?m=content&c=index&a=lists&catid=4 last;
rewrite ^/xydk/index_([0-9]+).html /index.php?m=content&c=index&a=lists&catid=4&page=$1 last;
rewrite ^/xydk/index_/([0-9]+).html /index.php?m=content&c=index&a=show&catid=4&id=$1 last;
rewrite ^/xydk/index_/([0-9]+)_([0-9]+).html /index.php?m=content&c=index&a=show&catid=4&id=$1&page=$2 last;
}
发布时间:2021/06/22
发表评论