1. 程式人生 > >php判斷手機段登錄,以及phpcms手機PC雙模板調用

php判斷手機段登錄,以及phpcms手機PC雙模板調用

mod pod 找不到 並且 mat pla sse 運行 highlight

首先一段php代碼判斷是否為手機瀏覽:

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;
    } 
    // 腦殘法,判斷手機發送的客戶端標誌,兼容性有待提高
    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‘
            ); 
        // 從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;
} 

phpcms 模板文件中創建兩個模板:

技術分享

其中 content中的文件是PC端模板,content_m中的文件是手機端模板。

其次在pgpcms中 編輯 phpcms/modules/content/index.php

講以上php代碼 放到index的class類中。

找到所有的:

include template(‘content‘,$template);

替換為:

if($this->isMobile()){
	include template(‘content_m‘,$template);
}else{
	include template(‘content‘,$template);

 最後將:

include template(‘content‘,‘index‘,$default_style);

 替換為:

if($this->isMobile()){
	include template(‘content_m‘,‘index‘,$default_style);
}else{
	include template(‘content‘,‘index‘,$default_style);
}

  

到現在為止基本上就設置好了當運行phpcms中的index.php文件是,會根據是否為手機端調用不同的模板,實現了手機端和PC端數據庫的同步。

php判斷手機段登錄,以及phpcms手機PC雙模板調用