萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 實戰教學:從靜態 HTML 到動態模板、計數器、留言板完整建站流程

專案目標:建立可維護的動態個人網站

從靜態 HTML 起步,逐步導入 PHP 模板系統、檔案計數器、留言表單,體驗「漸進式增強」的開發過程。

階段 1:靜態 HTML 原型

<!-- front.html -->
<!DOCTYPE html>
<html><head><title>我的個人主頁 - 歡迎</title></head>
<body>
    <h1>我的個人主頁</h1>
    <h2>歡迎</h2>
    <hr>
    <p>歡迎來我的寒舍...</p>
    <hr>
    <p>© 我自己, 1999</p>
</body></html>

問題:每頁重複 header/footer,修改極其痛苦。

階段 2:PHP 模板系統 — 共用版面與變數

目錄結構

project/
├── index.php
├── contact.php
├── include/
│   ├── common.php   # 共用變數
│   ├── header.php   # 頁首
│   └── footer.php   # 頁尾

共用變數 include/common.php

<?php
$siteName = 'PHP Talk\'s Home Page';
$myEmail  = 'phptalk@tnc.org';
$myEmailLink = '<a href="mailto:' . $myEmail . '">' . $myEmail . '</a>';
$myName = 'PHP Talk';

頁首 include/header.php

<?php // header.php -->
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
    <meta charset="UTF-8">
    <title> - </title>
</head>
<body>
    <h1></h1>
    <h2></h2>
    <hr>

頁尾 include/footer.php

<?php // footer.php -->
    <hr>
    <p align="center"><small><i>
        Copyright ©  (), 1999
    </i></small></p>
</body></html>

新首頁 index.php

<?php
$pageTitle = 'Welcome';
require_once 'include/common.php';
require_once 'include/header.php';
?>
<p>歡迎來我的寒舍...</p>
<p>不過我希望馬上就可以多起來。</p>
<?php require_once 'include/footer.php'; ?>

聯絡頁 contact.php

<?php
$pageTitle = 'Contact Information';
require_once 'include/common.php';
require_once 'include/header.php';
?>
<p>你可以透過 1-800-PHP-INFO 聯絡我</p>
<?php require_once 'include/footer.php'; ?>

階段 3:檔案型訪客計數器

include/counter.php

<?php
function getHitCount(string $file = 'counter.txt'): int
{
    $count = 0;
    if (file_exists($file)) {
        $fp = fopen($file, 'r');
        $count = (int)fgets($fp, 20);
        fclose($fp);
    }
    $count++;
    $fp = fopen($file, 'w');
    fputs($fp, (string)$count);
    fclose($fp);
    return $count;
}

首頁整合計數器

<?php
$pageTitle = 'Welcome';
require_once 'include/common.php';
require_once 'include/header.php';
require_once 'include/counter.php';
?>
<p>歡迎來我的寒舍...</p>
<p>總瀏覽:<strong></strong> 人次</p>
<?php require_once 'include/footer.php'; ?>

階段 4:留言回饋表單

表單頁 feedback.php

<?php
$pageTitle = 'Feedback';
require_once 'include/common.php';
require_once 'include/header.php';
?>
<form method="post" action="feedback_submit.php">
    <label>姓名:<input type="text" name="name" required></label><br>
    <label>Email:<input type="email" name="email" required></label><br>
    <label>留言:<textarea name="message" rows="5" required></textarea></label><br>
    <button type="submit">送出</button>
</form>
<?php require_once 'include/footer.php'; ?>

處理頁 feedback_submit.php

<?php
require_once 'include/common.php';
require_once 'include/header.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $message = trim($_POST['message'] ?? '');

    if ($name && $email && $message) {
        $to = $myEmail;
        $subject = "網站留言:{$name}";
        $body = "姓名:{$name}\nEmail:{$email}\n\n{$message}";
        $headers = "From: {$myEmail}\r\nReply-To: {$email}\r\nContent-Type: text/plain; charset=UTF-8";

        if (mail($to, $subject, $body, $headers)) {
            echo '<p>留言已送出,感謝您的回饋!</p>';
        } else {
            echo '<p style="color:red">發送失敗,請稍後再試。</p>';
        }
    } else {
        echo '<p style="color:red">請填寫所有欄位。</p>';
    }
}
require_once 'include/footer.php';

安全與維護補強

  • 輸出編碼:所有動態輸出用 `htmlspecialchars()` 防 XSS。
  • 輸入驗證:表單欄位長度、格式、內容過濾。
  • CSRF 保護:表單加入隱藏 Token,Session 驗證。
  • 計數器並發:檔案鎖 `LOCK_EX`、或改用 Redis `INCR`、資料庫原子更新。
  • 錯誤處理:`try-catch`、友善錯誤頁、記錄日誌。
  • 模板引擎升級:引入 Twig/Blade,享受繼承、區塊、自動跳脫、沙箱等進階功能。

結語

這個漸進式教學展示了 PHP 從「HTML 內嵌代碼」走向「模板分離、邏輯封裝」的演進路徑。掌握 `include` 機制、變數作用域、表單處理、郵件發送、檔案操作,即具備獨立開發中小型動態網站的核心能力。

copyright © 萬盛學電腦網 all rights reserved