純手工打造的 PHP 部落格系統,參考博客園風格,採用類 MVC 分層、自動載入、模板引擎分離,目標為展示完整後台管理流程。
-- 文章表
CREATE TABLE `post` (
`post_id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`category_id` VARCHAR(10) NOT NULL DEFAULT '', -- 逗號分隔多重分類
`type` ENUM('post','article','diary','postDraft','articleDraft') NOT NULL DEFAULT 'post',
`title` VARCHAR(200) NOT NULL,
`content` LONGTEXT NOT NULL,
`tags` VARCHAR(200) DEFAULT '',
`visible` TINYINT(1) NOT NULL DEFAULT 1,
`view_count` INT UNSIGNED DEFAULT 0,
`comment_count` INT UNSIGNED DEFAULT 0,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
FULLTEXT KEY `ft_title_content` (`title`, `content`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
-- 分類表 (支援多型別)
CREATE TABLE `category` (
`cat_id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`parent_id` INT UNSIGNED DEFAULT 0,
`type` ENUM('post','article','diary','album') NOT NULL DEFAULT 'post',
`name` VARCHAR(50) NOT NULL,
`description` VARCHAR(255) DEFAULT '',
`visible` TINYINT(1) DEFAULT 1,
`created_at` DATETIME NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
-- 留言表
CREATE TABLE `comment` (
`comment_id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`post_id` INT UNSIGNED NOT NULL,
`author` VARCHAR(50) NOT NULL,
`email` VARCHAR(100) NOT NULL,
`content` TEXT NOT NULL,
`ip` VARCHAR(45) NOT NULL,
`user_agent` VARCHAR(255) DEFAULT '',
`created_at` DATETIME NOT NULL,
`status` TINYINT(1) DEFAULT 1
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
admin/ ├── config/ │ └── config.php # 常量、自動載入、密碼雜湊 ├── classes/ │ ├── Database.class.php # PDO 封裝 │ ├── Pager.class.php # 分頁 │ └── Model/ # 業務模型 ├── templates/ # PHP 原生模板 ├── extensions/ # TinyMCE 等第三方 ├── upload/ # 上傳檔案 ├── index.php # 總控制器 ├── post.php # 文章管理 ├── article.php # 專欄文章 ├── photo.php # 相簿 └── comment.php # 留言管理
<?php
// config.php
ini_set('display_errors', 1);
date_default_timezone_set('Asia/Taipei');
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(dirname(__FILE__)));
define('DB_USER', 'blog_user');
define('DB_PASS', 'secret');
define('DB_NAME', 'blog');
define('CLASS_PATH', ROOT . DS . 'classes');
define('TPL_PATH', ROOT . DS . 'templates');
// 密碼雜湊 (Blowfish/crypt)
define('ADMIN_USER', 'admin');
define('ADMIN_PASS_HASH', '$2y$10$...'); // password_hash() 產生
function hasher(string $pwd, string $hash = ''): string|bool
{
if ($hash) return password_verify($pwd, $hash);
return password_hash($pwd, PASSWORD_BCRYPT, ['cost' => 10]);
}
spl_autoload_register(function ($class) {
$file = CLASS_PATH . DS . strtolower($class) . '.class.php';
if (is_file($file)) require_once $file;
});
?>
<?php
require_once 'config/config.php';
session_start();
$action = $_GET['action'] ?? '';
$username = $_SESSION['admin_user'] ?? '';
if ($action !== 'login' && $action !== 'logout' && !$username) {
login(); exit;
}
match ($action) {
'login' => login(),
'logout' => logout(),
default => dashboard(),
};
function login() {
$data['title'] = '後台登入';
if (isset($_POST['login'])) {
if ($_POST['user'] === ADMIN_USER && hasher($_POST['pass'], ADMIN_PASS_HASH)) {
$_SESSION['admin_user'] = ADMIN_USER;
header('Location: index.php'); exit;
}
$data['error'] = '帳號或密碼錯誤';
}
include TPL_PATH . '/login.php';
}
function dashboard() {
$data['title'] = '管理首頁';
// 統計資料...
include TPL_PATH . '/dashboard.php';
}
function logout() {
session_destroy();
header('Location: index.php');
}
?>
<!DOCTYPE html>
<html><head><title>= htmlspecialchars($title) ?></title></head>
<body>
<header><h1>= $title ?></h1></header>
<main>
<p>文章總數: = $stats['posts'] ?? 0 ?></p>
<p>留言總數: = $stats['comments'] ?? 0 ?></p>
</main>
</body></html>