现在随着智能手机的兴起,我们网站开发移动端版本也显得越来重要了,但是现在phpcms还没有开发手机版,下面我们就教大家如何让phpcms实现手机版访问,一般正常情况下我们电脑版的域名为http://www.你的域名/,而手机版的域名一般为http://m.你的域名/
这样就需要我们给手机端另外做一套模板,实现网站双模板。下面是具体方法:
首先给phpcms添加自适配的代码,找到并打开/modules/content/index.php文件,在里面找到如下代码:
1
include template('content',$template);
将其修改为如下代码:
1
if(substr($_SERVER['SERVER_NAME'], 0,1) =='m'){
2
include template('content_m',$template);
3
}else{
4
include template('content',$template);
5
}
如果不想手动修改,可以直接使用我们已经修改好的文件下载覆盖就可以了,下载地址如下:
以上代码的意思是当前页面url中第一个字符为m时则调用content_m模板,否则调用content模板
但是由于phpcms把文章的url都固定写死在数据表中,所以页面中的标签不能在使用{$r[url]}
而要改成{str_replace('http://www.','http://m.',$r[url])}
意思是截取url,把http://www.你的域名/ 替换成http://m.你的域名/
这里我们就完成了手机版的设置了,然后我们在制作一套手机端模板content_m就可以了。
如果我们要在PC端的内容里面加上当前页面手机端的链接,链接地址写法如下:
1
http://{str_replace('www.','m.',$_SERVER['SERVER_NAME'])}{$_SERVER['REQUEST_URI']}
反之,手机端加上PC端的链接:
1
http://{str_replace('m.','www.',$_SERVER['SERVER_NAME'])}{$_SERVER['REQUEST_URI']}
如果你使用的是静态页面,那么只要在模板页头加上以下JS代码就可以实现判断手机端自动跳转到手机端了。
01
02
function browserRedirect() {
03
var sUserAgent = navigator.userAgent.toLowerCase();
04
var bIsIpad = sUserAgent.match(/ipad/i) =="ipad";
05
var bIsIphoneOs = sUserAgent.match(/iphone os/i) =="iphone os";
06
var bIsMidp = sUserAgent.match(/midp/i) =="midp";
07
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) =="rv:1.2.3.4";
08
var bIsUc = sUserAgent.match(/ucweb/i) =="ucweb";
09
var bIsAndroid = sUserAgent.match(/android/i) =="android";
10
var bIsCE = sUserAgent.match(/windows ce/i) =="windows ce";
11
var bIsWM = sUserAgent.match(/windows mobile/i) =="windows mobile";
12
if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
13
{if $catid=='' and $id==''}
14
window.location.href="{APP_PATH}/index.php";
15
{elseif $id=='' and $catid!=''}
16
window.location.href="{APP_PATH}/index.php?m=content&c=index&a=lists&catid={$catid}";
17
{else}
18
window.location.href="{APP_PATH}/index.php?m=content&c=index&a=show&catid={$catid}&id={$id}";
19
{/if}
20
}
21
}
22
browserRedirect();
23
24
function closewindow() {
25
$("#register-box").hide();
26
}
27
function openwindow() {
28
$("#register-box").show();
29
}
30