我們將建立一個包含首頁、聯絡頁、簡歷頁的個人網站,並實作共用版面、訪客計數器、留言表單等核心功能。專案結構:
project/
├── index.php # 首頁
├── contact.php # 聯絡頁
├── resume.php # 簡歷頁
├── include/
│ ├── common.php # 共用變數
│ ├── header.php # 頁首模板
│ ├── footer.php # 頁尾模板
│ └── counter.php # 計數器函式
└── data/
└── counter.txt # 計數器資料檔
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' => '簡歷'],
];
include/header.php<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>= SITE_NAME ?> - = htmlspecialchars($pageTitle ?? '') ?></title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>= SITE_NAME ?></h1>
<nav>
<ul>
<li><a href="= $item['href'] ?>" = ($item['href'] === basename($_SERVER['PHP_SELF'])) ? 'class="active"' : '' ?>>= $item['text'] ?></a></li>
</ul>
</nav>
</header>
<main>
include/footer.php
</main>
<footer>
<hr>
<p class="copyright">© = COPYRIGHT_YEAR ?> = SITE_NAME ?> | <a href="mailto:= ADMIN_EMAIL ?>">聯絡我</a></p>
</footer>
</body>
</html>
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;
}
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>= getHitCount() ?></strong> 人次</p>
</aside>
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>= htmlspecialchars($msg) ?></p>
</div>
<form method="post" action="">
<div>
<label for="name">姓名</label>
<input type="text" id="name" name="name" value="= htmlspecialchars($name ?? '') ?>" required>
</div>
<div>
<label for="email">Email</label>
<input type="email" id="email" name="email" value="= htmlspecialchars($email ?? '') ?>" required>
</div>
<div>
<label for="content">留言內容</label>
<textarea id="content" name="content" rows="6" required>= htmlspecialchars($content ?? '') ?></textarea>
</div>
<button type="submit">送出</button>
</form>
</section>
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>
透過 include 機制實作簡易模板系統,掌握 PHP 「邏輯與視圖分離」的核心觀念。這個專案雖小,但包含路由、模板、資料儲存、表單處理、安全基礎等完整 Web 開發要素,是進階框架學習的最佳墊腳石。