基於 GET 參數傳遞用戶 ID 實現簡單的站內信/郵件通知系統。本文展示完整的發送、列表、查看、登錄、登出流程,適合理解基礎 Web 應用狀態管理。
| 文件 | 功能 | 核心邏輯 |
|---|---|---|
| reg.php | 用戶/評論列表 | 查詢 comments 表,生成查看/發送鏈接 |
| newfile.php | 詳情 + 發送入口 | 顯示內容,生成發送郵件鏈接,檢查新郵件 |
| send_email.php | 發送處理 | 寫入 email 表,標記 flag='no' |
| see_email.php | 查看郵件 | 查詢 email 表,更新 flag='yes' |
| login.php | 模擬登錄 | 設置 $_SESSION['id'] |
| delete_session.php | 登出 | 銷毀 Session |
CREATE TABLE `comments` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`content` TEXT NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE `email` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`sender_id` INT NOT NULL,
`accepter_id` INT NOT NULL,
`flag` ENUM('no','yes') DEFAULT 'no',
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
<?php
require_once 'db.php'; // PDO 連接
session_start();
$stmt = $pdo->query("SELECT id, content, created_at FROM comments ORDER BY created_at DESC");
$comments = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<h2>評論列表</h2>
<ul>
<?php foreach ($comments as $c): ?>
<li>
<strong></strong>
<small></small>
<a href="newfile.php?id=>查看詳情/發送郵件</a>
</li>
<?php endforeach; ?>
</ul>
<?php
require_once 'db.php';
session_start();
$id = (int)($_GET['id'] ?? 0);
if (!$id) die('無效 ID');
// 獲取評論內容
$stmt = $pdo->prepare("SELECT * FROM comments WHERE id = ?");
$stmt->execute([$id]);
$comment = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$comment) die('評論不存在');
// 檢查當前用戶是否有新郵件
$hasNewMail = false;
if (isset($_SESSION['user_id'])) {
$stmt = $pdo->prepare("SELECT 1 FROM email WHERE accepter_id = ? AND flag = 'no' LIMIT 1");
$stmt->execute([$_SESSION['user_id']]);
$hasNewMail = (bool)$stmt->fetchColumn();
}
?>
<h3></h3>
<small></small>
<hr>
<a href="send_email.php?id=>發送郵件給作者</a>
<?php if ($hasNewMail): ?>
<strong style="color:red"><a href="see_email.php">您有新郵件!</a></strong>
<?php endif; ?>
<?php
require_once 'db.php';
session_start();
if (!isset($_SESSION['user_id'])) {
die('請先登錄');
}
$recipientId = (int)($_GET['id'] ?? 0);
if (!$recipientId) die('無效接收者');
$stmt = $pdo->prepare("INSERT INTO email (sender_id, accepter_id, flag) VALUES (?, ?, 'no')");
$stmt->execute([$_SESSION['user_id'], $recipientId]);
echo "<script>alert('發送成功'); window.location.href='reg.php';</script>";
<?php session_start(); // 實際項目應驗證用戶名密碼 $_SESSION['user_id'] = 15; // 模擬用戶 ID echo "登錄成功 <a href='reg.php'>查看列表</a> | <a href='delete_session.php'>登出</a>";
<?php
session_start();
session_destroy();
echo "<script>alert('已登出'); window.location.href='login.php';</script>";
htmlspecialchars()GET /comments # 列表
GET /comments/{id} # 詳情
POST /comments/{id}/mail # 發送郵件
GET /mail/inbox # 收件箱
PATCH /mail/{id}/read # 標記已讀
function jsonResponse($data, $code = 200) {
http_response_code($code);
header('Content-Type: application/json');
echo json_encode(['code' => $code, 'data' => $data]);
exit;
}
class MailService {
public function send(int $from, int $to, string $content): bool { /* ... */ }
public function getInbox(int $userId, int $page = 1): array { /* ... */ }
public function markRead(int $mailId, int $userId): bool { /* ... */ }
}
這個簡單示例展示了 Web 應用的核心要素:路由、控制器、服務、數據庫、會話、安全。實際項目中應按層次組織代碼。