首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 靈活參數傳遞:URL 查詢字串實作文章評論連結

     編輯:PHP 相關知識

利用 GET 參數實現動態內容頁面的基礎範例

早期 PHP 開發中,透過 URL 查詢字串 (?id=123) 傳遞文章識別碼至評論頁面是最直觀的參數傳遞方式。本文展示基本實作與現代改良建議。

基本實作流程

1. 文章列表頁 (index.php) - 產生帶參數連結

<?php
// 假設從資料庫讀取文章列表
$articles = [
    ['id' => 1, 'title' => 'PHP 入門教學'],
    ['id' => 2, 'title' => 'MySQL 基礎操作'],
    ['id' => 3, 'title' => 'Apache 配置技巧'],
];
?>
<html>
<body>
<h1>文章列表</h1>
<ul>
<?php foreach ($articles as $art): ?>
    <li>
        <a href="article.php?id=<?= $art['id'] ?>">
            <?= htmlspecialchars($art['title']) ?>
        </a>
        | 
        <a href="comments.php?id=<?= $art['id'] ?>">
            查看評論
        </a>
    </li>
<?php endforeach; ?>
</ul>
</body>
</html>

2. 文章內容頁 (article.php) - 接收參數顯示內容

<?php
$id = $_GET['id'] ?? 0;
// 實際專案應使用 Prepared Statements 防 SQL 注入
$stmt = $pdo->prepare("SELECT * FROM articles WHERE id = ?");
$stmt->execute([$id]);
$article = $stmt->fetch();

if (!$article) {
    http_response_code(404);
    die("文章不存在");
}
?>
<html>
<body>
<h1><?= htmlspecialchars($article['title']) ?></h1>
<div><?= nl2br(htmlspecialchars($article['content'])) ?></div>
<p><a href="comments.php?id=<?= $id ?>">前往評論區</a></p>
</body>
</html>

3. 評論頁面 (comments.php) - 同一參數不同邏輯

<?php
$id = $_GET['id'] ?? 0;
$stmt = $pdo->prepare("SELECT * FROM comments WHERE article_id = ? ORDER BY created_at DESC");
$stmt->execute([$id]);
$comments = $stmt->fetchAll();

// 同時取得文章標題供顯示
$stmt = $pdo->prepare("SELECT title FROM articles WHERE id = ?");
$stmt->execute([$id]);
$article = $stmt->fetch();
?>
<html>
<body>
<h1><?= htmlspecialchars($article['title']) ?> - 讀者評論</h1>
<ul>
<?php foreach ($comments as $c): ?>
    <li>
        <strong><?= htmlspecialchars($c['author']) ?></strong>:
        <?= htmlspecialchars($c['content']) ?>
        <small><?= $c['created_at'] ?></small>
    </li>
<?php endforeach; ?>
</ul>
</body>
</html>

參數傳遞方式對比

方式範例優點缺點適用場景
GET 查詢字串page.php?id=123可書籤、可分享、SEO 友善、瀏覽器歷史記錄長度限制、參數暴露、不適合敏感資料文章閱讀、搜尋、分頁、公開資源
POST 表單form action="page.php" method="POST"無長度限制、參數不在 URL、適合敏感操作不可書籤、重新整理會重複提交登入、發表評論、修改資料、上傳檔案
Session$_SESSION['article_id'] = 123跨頁面持久、伺服器端儲存依賴 Cookie、多標籤頁狀態同步問題多步驟精靈、購物車、用戶登入狀態
URL 路徑參數 (REST)/article/123/comments語意清晰、SEO 最佳、API 標準需 Web Server Rewrite 規則支援現代框架 (Laravel/Symfony)、REST API

安全性關鍵防護

  • SQL 注入:絕對禁止 直接拼接 "SELECT * FROM t WHERE id=$id"。必須使用 Prepared Statements (PDO/mysqli)
  • XSS 攻擊:所有輸出 HTML 內容必須 htmlspecialchars() 轉義
  • ID 遍歷洩露:若需隱藏資料量,使用 UUID、Hashids 或 Slug 替代自增 ID
  • 授權檢查:評論刪除/編輯必須驗證當前用戶擁有權限,而非僅靠 ID

現代框架路由寫法對照 (Laravel 範例)


// routes/web.php
Route::get('/article/{id}', 'ArticleController@show')->name('article.show');
Route::get('/article/{id}/comments', 'CommentController@index')->name('article.comments');

// Controller
public function show($id) {
    $article = Article::findOrFail($id); // 自動 404
    return view('article.show', compact('article'));
}

// Blade 模板產生連結
<a href="{{ route('article.comments', $article->id) }}">查看評論</a>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved