
Admin
Well-Known Member
Staff member
Administrator
Đến lúc xem file .htaccess của cái framework CI thì tự nhiên nảy ra ý định làm cái tự động chèn header và footer. Sau đây là code(gồm 2 file chính):
file .htaccess
file index.php
Hướng dẫn sử dụng
Bạn tạo 2 file index.php và .htaccess ở thư mục gốc(root) và chèn code như trên vào 2 file tương ứng. Sau đó bạn tạo tiếp các file header.php, footer.php, 404.php. Chức năng để làm gì thì các bạn biết rồi đấy.
Tất cả chỉ đơn giản vậy thôi.
Nguồn: http://cugiai.com/archives/187
file .htaccess
PHP:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteCond %{REQUEST_FILENAME}% !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
PHP:
<?php
/*
@___VUI LONG GIU NGUYEN BAN QUYEN___@
@___Tool auto header & footer___@
@___Author: datproga10x___@
@___Website: http://cugiai.com___@
@___Create date: 6-3-2013___@
*/
//tao $rt va dat gia tri $rt = $_GET['rt'];
$rt = $_GET['rt'];
//chen file header
include 'header.php';
//kiem tra $rt
//neu chua co $rt hoac $rt = NULL
//thi chen file index.htm o root
if(!isset($rt) || $rt==NULL) {
include 'index.htm';
}else{
//kiem tra xem $rt co phai folder hay khong
//neu khong thi include file index.htm da co o folder do
//neu $rt la file thi include file do
if(is_dir($rt)) {
include $rt.'index.htm';
}
if(is_file($rt)) {
include $rt;
}
//neu $rt khong phai la file hoac folder
//include file 404.htm o root
if(!is_dir($rt) && !is_file($rt)) {
include '404.htm';
}
}
//chen file footer
include 'footer.php';
?>
Bạn tạo 2 file index.php và .htaccess ở thư mục gốc(root) và chèn code như trên vào 2 file tương ứng. Sau đó bạn tạo tiếp các file header.php, footer.php, 404.php. Chức năng để làm gì thì các bạn biết rồi đấy.
Tất cả chỉ đơn giản vậy thôi.
Nguồn: http://cugiai.com/archives/187