如何安全地在页面间通过 URL 参数传递特定用户数据?

2026-05-07 07:362阅读0评论SEO资讯
  • 内容介绍
  • 相关推荐

本文共计1035个文字,预计阅读时间需要5分钟。

如何安全地在页面间通过 URL 参数传递特定用户数据?

原文:

在 PHP Web 开发中,常需从数据列表页(如用户表格)跳转至详情或报告页,并携带当前行的唯一标识(如 id)。你当前的代码存在两个关键问题:一是 <a> 标签中错误嵌套了 </a> 闭合标签,且 onclick="documents('<?= $id?>')" 调用的 JavaScript 函数 documents() 仅尝试调用 document.getElementById(),但该方法用于获取 DOM 元素而非发送数据;二是直接将密码等敏感字段拼入 URL 或前端展示,严重违反安全规范。

正确的做法是:使用 URL 查询参数(GET)传递必要且安全的数据。URL 参数以 ? 开始,键值对格式为 key=value,多个参数用 & 连接。例如:

<td> <a href="anotherpage.php?id=<?= urlencode($smth['id']) ?>&name=<?= urlencode($smth['name']) ?>" class="btn btn-primary" target="_blank" role="button" > 生成报告 </a> </td>

<?php if (isset($_POST['service1'])): ?> <?php $sql = "SELECT id, name FROM users"; // ❗移除 password 字段,前端无需且不应接触 $result = conection($sql); ?> <table class="table table-striped" style="width:100%"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>操作</th> </tr> </thead> <tbody> <?php foreach ($result as $smth): ?> <tr> <td><?= htmlspecialchars($smth['id']) ?></td> <td><?= htmlspecialchars($smth['name']) ?></td> <td> <a href="anotherpage.php?id=<?= urlencode($smth['id']) ?>&name=<?= urlencode($smth['name']) ?>" class="btn btn-sm btn-primary" target="_blank" > 生成 PDF 报告 </a> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php endif; ?>

在 anotherpage.php(即 PDF 生成页)中,通过 $_GET 获取参数并进行基础校验与转义:

<?php // 严格验证和过滤输入 $id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); $name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_STRING); if (!$id || empty($name)) { die('参数错误:缺少有效的用户 ID 或姓名'); } // 安全查询数据库(使用预处理语句防范 SQL 注入) $sql = "SELECT id, name, email FROM users WHERE id = ?"; $stmt = $pdo->prepare($sql); // 假设已建立 PDO 连接 $stmt->execute([$id]); $user = $stmt->fetch(PDO::FETCH_ASSOC); if (!$user) { die('未找到对应用户'); } ?>

接着集成 FPDF(注意:需确保 fpdf.php 路径正确,且类定义后立即实例化):

<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/Libraries/fpdf.php'; class PDF extends FPDF { private $user; // 存储用户数据,避免全局变量 function __construct($user) { parent::__construct('L', 'mm', 'A4'); $this->user = $user; } function Header() { $this->SetFillColor(0, 90, 151); $this->SetTextColor(255); $this->SetDrawColor(0, 90, 151); $this->SetLineWidth(0.5); $this->SetFont('Arial', 'B', 16); // 居中标题 $this->Cell(0, 10, '用户报告', 0, 1, 'C'); $this->Ln(5); // 用户信息区块 $this->SetFont('Arial', '', 12); $this->Cell(30, 8, 'ID:', 0, 0, 'L'); $this->Cell(0, 8, $this->user['id'], 0, 1, 'L'); $this->Cell(30, 8, '姓名:', 0, 0, 'L'); $this->Cell(0, 8, htmlspecialchars($this->user['name']), 0, 1, 'L'); $this->Cell(30, 8, '邮箱:', 0, 0, 'L'); $this->Cell(0, 8, htmlspecialchars($this->user['email'] ?? 'N/A'), 0, 1, 'L'); $this->Ln(10); } } // 实例化 PDF 并输出 $pdf = new PDF($user); $pdf->AddPage(); $pdf->Output('I', 'report_' . $user['id'] . '.pdf'); // I = 浏览器内联显示 ?>

? 关键总结

  • ✅ 使用 $_GET + URL 参数是跨页传递轻量、非敏感数据的标准方式;
  • ✅ 始终对输出到 HTML 的变量使用 htmlspecialchars(),对 URL 参数使用 urlencode();
  • ✅ 对输入参数执行 filter_input() 验证,对数据库查询使用预处理语句;
  • ❌ 绝对禁止在 URL、HTML 源码或日志中暴露密码、令牌、身份证号等敏感字段;
  • ? 如需传递敏感上下文(如权限状态),应改用服务端 Session + 唯一任务 Token,而非客户端可见参数。

遵循以上模式,即可安全、可靠地实现“点击某行 → 生成专属 PDF”的业务流程。

本文共计1035个文字,预计阅读时间需要5分钟。

如何安全地在页面间通过 URL 参数传递特定用户数据?

原文:

在 PHP Web 开发中,常需从数据列表页(如用户表格)跳转至详情或报告页,并携带当前行的唯一标识(如 id)。你当前的代码存在两个关键问题:一是 <a> 标签中错误嵌套了 </a> 闭合标签,且 onclick="documents('<?= $id?>')" 调用的 JavaScript 函数 documents() 仅尝试调用 document.getElementById(),但该方法用于获取 DOM 元素而非发送数据;二是直接将密码等敏感字段拼入 URL 或前端展示,严重违反安全规范。

正确的做法是:使用 URL 查询参数(GET)传递必要且安全的数据。URL 参数以 ? 开始,键值对格式为 key=value,多个参数用 & 连接。例如:

<td> <a href="anotherpage.php?id=<?= urlencode($smth['id']) ?>&name=<?= urlencode($smth['name']) ?>" class="btn btn-primary" target="_blank" role="button" > 生成报告 </a> </td>

<?php if (isset($_POST['service1'])): ?> <?php $sql = "SELECT id, name FROM users"; // ❗移除 password 字段,前端无需且不应接触 $result = conection($sql); ?> <table class="table table-striped" style="width:100%"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>操作</th> </tr> </thead> <tbody> <?php foreach ($result as $smth): ?> <tr> <td><?= htmlspecialchars($smth['id']) ?></td> <td><?= htmlspecialchars($smth['name']) ?></td> <td> <a href="anotherpage.php?id=<?= urlencode($smth['id']) ?>&name=<?= urlencode($smth['name']) ?>" class="btn btn-sm btn-primary" target="_blank" > 生成 PDF 报告 </a> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php endif; ?>

在 anotherpage.php(即 PDF 生成页)中,通过 $_GET 获取参数并进行基础校验与转义:

<?php // 严格验证和过滤输入 $id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); $name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_STRING); if (!$id || empty($name)) { die('参数错误:缺少有效的用户 ID 或姓名'); } // 安全查询数据库(使用预处理语句防范 SQL 注入) $sql = "SELECT id, name, email FROM users WHERE id = ?"; $stmt = $pdo->prepare($sql); // 假设已建立 PDO 连接 $stmt->execute([$id]); $user = $stmt->fetch(PDO::FETCH_ASSOC); if (!$user) { die('未找到对应用户'); } ?>

接着集成 FPDF(注意:需确保 fpdf.php 路径正确,且类定义后立即实例化):

<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/Libraries/fpdf.php'; class PDF extends FPDF { private $user; // 存储用户数据,避免全局变量 function __construct($user) { parent::__construct('L', 'mm', 'A4'); $this->user = $user; } function Header() { $this->SetFillColor(0, 90, 151); $this->SetTextColor(255); $this->SetDrawColor(0, 90, 151); $this->SetLineWidth(0.5); $this->SetFont('Arial', 'B', 16); // 居中标题 $this->Cell(0, 10, '用户报告', 0, 1, 'C'); $this->Ln(5); // 用户信息区块 $this->SetFont('Arial', '', 12); $this->Cell(30, 8, 'ID:', 0, 0, 'L'); $this->Cell(0, 8, $this->user['id'], 0, 1, 'L'); $this->Cell(30, 8, '姓名:', 0, 0, 'L'); $this->Cell(0, 8, htmlspecialchars($this->user['name']), 0, 1, 'L'); $this->Cell(30, 8, '邮箱:', 0, 0, 'L'); $this->Cell(0, 8, htmlspecialchars($this->user['email'] ?? 'N/A'), 0, 1, 'L'); $this->Ln(10); } } // 实例化 PDF 并输出 $pdf = new PDF($user); $pdf->AddPage(); $pdf->Output('I', 'report_' . $user['id'] . '.pdf'); // I = 浏览器内联显示 ?>

? 关键总结

  • ✅ 使用 $_GET + URL 参数是跨页传递轻量、非敏感数据的标准方式;
  • ✅ 始终对输出到 HTML 的变量使用 htmlspecialchars(),对 URL 参数使用 urlencode();
  • ✅ 对输入参数执行 filter_input() 验证,对数据库查询使用预处理语句;
  • ❌ 绝对禁止在 URL、HTML 源码或日志中暴露密码、令牌、身份证号等敏感字段;
  • ? 如需传递敏感上下文(如权限状态),应改用服务端 Session + 唯一任务 Token,而非客户端可见参数。

遵循以上模式,即可安全、可靠地实现“点击某行 → 生成专属 PDF”的业务流程。