萬盛學電腦網

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

從零開始建構 PHP 個人網站:模板系統、計數器與表單處理實戰

專案規劃:簡單而完整的個人站點

我們將建立一個包含首頁、聯絡頁、簡歷頁的個人網站,並實作共用版面、訪客計數器、留言表單等核心功能。專案結構:

project/
├── index.php          # 首頁
├── contact.php        # 聯絡頁
├── resume.php         # 簡歷頁
├── include/
│   ├── common.php     # 共用變數
│   ├── header.php     # 頁首模板
│   ├── footer.php     # 頁尾模板
│   └── counter.php    # 計數器函式
└── data/
    └── counter.txt    # 計數器資料檔

步驟 1:定義共用變數 include/common.php

<?php
// 站點基本設定
define('SITE_NAME', '我的個人首頁');
define('ADMIN_EMAIL', 'me@example.com');
define('COPYRIGHT_YEAR', 2024);

// 導覽選單
$navItems = [
    ['href' => 'index.php', 'text' => '首頁'],
    ['href' => 'contact.php', 'text' => '聯絡我'],
    ['href' => 'resume.php', 'text' => '簡歷'],
];

步驟 2:建立頁首模板 include/header.php

<!DOCTYPE html>
<html lang="zh-Hant">
<head>
    <meta charset="UTF-8">
    <title> - </title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <header>
        <h1></h1>
        <nav>
            <ul>
                
                    <li><a href="" ></a></li>
                
            </ul>
        </nav>
    </header>
    <main>

步驟 3:建立頁尾模板 include/footer.php


    </main>
    <footer>
        <hr>
        <p class="copyright">©   | <a href="mailto:">聯絡我</a></p>
    </footer>
</body>
</html>

步驟 4:實作檔案型計數器 include/counter.php

<?php
function getHitCount(string $file = 'data/counter.txt'): int
{
    $count = 0;
    if (file_exists($file)) {
        $count = (int)file_get_contents($file);
    }
    $count++;
    file_put_contents($file, $count, LOCK_EX);
    return $count;
}

步驟 5:首頁 index.php

<?php
$pageTitle = '歡迎光臨';
require_once 'include/common.php';
require_once 'include/header.php';
require_once 'include/counter.php';
?>
<section class="welcome">
    <h2>歡迎來到我的個人網站</h2>
    <p>這裡記錄我的學習心得、專案作品與生活點滴。</p>
</section>

<aside class="stats">
    <p>本站總瀏覽次數:<strong></strong> 人次</p>
</aside>

步驟 6:聯絡表單 contact.php

<?php
$pageTitle = '聯絡我';
require_once 'include/common.php';
require_once 'include/header.php';

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

    if ($name === '' || $email === '' || $content === '') {
        $messages[] = '請填寫所有必填欄位。';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $messages[] = 'Email 格式不正確。';
    } else {
        // 實際專案應寫入資料庫或發送郵件
        $messages[] = '感謝您的留言,我會儘快回覆。';
        // 清空表單
        $name = $email = $content = '';
    }
}
?>
<section class="contact">
    <h2>聯絡表單</h2>
    
        <div class="messages">
            
                <p></p>
            
        </div>
    
    <form method="post" action="">
        <div>
            <label for="name">姓名</label>
            <input type="text" id="name" name="name" value="" required>
        </div>
        <div>
            <label for="email">Email</label>
            <input type="email" id="email" name="email" value="" required>
        </div>
        <div>
            <label for="content">留言內容</label>
            <textarea id="content" name="content" rows="6" required></textarea>
        </div>
        <button type="submit">送出</button>
    </form>
</section>

步驟 7:簡歷頁 resume.php

<?php
$pageTitle = '個人簡歷';
require_once 'include/common.php';
require_once 'include/header.php';
?>
<section class="resume">
    <h2>工作經歷</h2>
    <article>
        <h3>資深 PHP 工程師 | ABC 科技</h3>
        <time>2020 - 至今</time>
        <ul>
            <li>主導 Laravel 微服務架構重構</li>
            <li>建立 CI/CD 流程,部署時間縮短 80%</li>
        </ul>
    </article>
    <!-- 更多經歷... -->
</section>

進階擴充建議

  • 資料庫取代文字檔:計數器、留言改用 SQLite / MySQL,支援並發與查詢。
  • 引入模板引擎:Twig、Blade、Plates,分離邏輯與視圖。
  • 加入 PSR-4 自動載入:使用 Composer 管理依賴,類別檔案自動載入。
  • 安全強化:CSRF Token、輸入驗證、輸出編碼、HSTS、CSP 標頭。
  • 部署自動化:GitHub Actions / GitLab CI 自動測試、部署至伺服器。

總結

透過 include 機制實作簡易模板系統,掌握 PHP 「邏輯與視圖分離」的核心觀念。這個專案雖小,但包含路由、模板、資料儲存、表單處理、安全基礎等完整 Web 開發要素,是進階框架學習的最佳墊腳石。

copyright © 萬盛學電腦網 all rights reserved