程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> ThinkPHP搭建手機版網站

ThinkPHP搭建手機版網站

編輯:關於PHP編程

代碼中的函數 is_mobile來源於網上,寫得挺好的。入口文件通過判斷終端類型,定義APP_PATH和APP_NAME,從而執行不同的分組。我要實現的效果是PC和Mobile均用頂級域名訪問。當然了,如果你想在手機裡實現類似於wap.xxx.com這樣的訪問,本方法是做不到的,這是thinkphp多域名部署的問題,具體參見tp手冊。
用thinkphp框架快速開發一個小型站點還是很方便的,正如本站。菜根網還做了一個手機端的網站,手機浏覽器輸入本站域名www.icaigen.com查看效果。

下面僅僅講述在thinkphp的前端引導頁面index.php文件中實現平台判斷,從而加載不同的分組(或項目)。

//判斷是否屬手機

function is_mobile(){

    $user_agent = $_SERVER['HTTP_USER_AGENT'];

    $mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");

    $is_mobile = false;

    foreach ($mobile_agents as $device) {

        if (stristr($user_agent, $device)) {

            $is_mobile = true;

            break;

        }

    }

    return $is_mobile;

}

define('THINK_PATH','./CORE/');

if(is_mobile()){ //跳轉至wap分組

    define('APP_NAME','Wap');

    define('APP_PATH','./Wap/');

}else{

    define('APP_NAME','Home');

    define('APP_PATH','./Home/');           

}

define('APP_DEBUG', false);

require THINK_PATH.'core.php';
代碼中的函數 is_mobile來源於網上,寫得挺好的。入口文件通過判斷終端類型,定義APP_PATH和APP_NAME,從而執行不同的分組。

我要實現的效果是PC和Mobile均用頂級域名訪問。當然了,如果你想在手機裡實現類似於wap.xxx.com這樣的訪問,本方法是做不到的,這是thinkphp多域名部署的問題,具體參見tp手冊。

定義好了入口文件,然後就可以在WAP分組(或項目)下開發手機版網站的代碼了。因為是手機版網站,分辨率,圖片壓縮,網頁大小等都是赤裸裸的問題。本人也才接觸手機網站建設沒幾天,就不賣弄了。可以明確的是,PC端網站上非主要的功能在手機網站上都可以砍掉了,比如博客的熱門文章,隨機文章,評論列表,各種掛件等。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved