:::
10-1 製作頁首、頁尾檔
一、為何需要頁首、頁尾檔?
- 因為檔案的前後常常是重複的
- 為了避免以後要修改一堆檔案
二、頁首檔 header.php
- 把 index.php 和 admin.php 上方相同的部份取出
- 存成 header.php
<?php session_start(); require_once "config.php"; require_once "function.php"; require_once 'smarty/libs/Smarty.class.php'; $smarty = new Smarty; $db = link_db(); $op = isset($_REQUEST['op']) ? htmlspecialchars($_REQUEST['op'], ENT_QUOTES) : ''; $group = isset($_SESSION['group']) ? $_SESSION['group'] : ''; $name = isset($_SESSION['name']) ? $_SESSION['name'] : '訪客'; $content = $error = '';
- 最後用 require_once 引入該檔即可(index.php也要比照辦理)
<?php require_once "header.php"; if ($group != 'admin') { header("location: index.php"); exit; }
三、頁尾檔 footer.php
- 把 index.php 和 admin.php 下方相同的部份取出
- 存成 footer.php
<?php $smarty->assign('name', $name); $smarty->assign('group', $group); $smarty->assign('content', $content); $smarty->assign('op', $op); $smarty->assign('error', $error); $smarty->assign('page_title', $page_title); $smarty->display('index.tpl'); - 其中,為了讓 page_title 可以每頁不同,所以,將之設成變數。因此,我們可以在引入頁尾前,設定一下該變數
- 最後用 require_once 引入該檔即可(index.php也要比照辦理)
try { switch ($op) { default: $content = action_form(); break; } } catch (exception $e) { $error = $e->getMessage(); } $page_title = '活動管理'; require_once "footer.php";
10. 製作活動管理頁面