<?php
declare(strict_types=1);
$cases = [];
function add_case(array &$cases, string $name, mixed $expected, mixed $actual): void {
$cases[] = [
'name' => $name,
'passed' => ($actual === $expected),
'expected' => $expected,
'actual' => $actual,
];
}
$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('CREATE TABLE Websites (id INTEGER PRIMARY KEY, name TEXT, url TEXT, alexa INTEGER, country TEXT)');
$ins = $pdo->prepare('INSERT INTO Websites (id, name, url, alexa, country) VALUES (?, ?, ?, ?, ?)');
foreach ([
[1, 'Google', 'https://www.google.com/', 1, 'USA'],
[2, '淘宝', 'https://www.taobao.com/', 13, 'CN'],
[3, '蓝色天空网', 'https://www.mbsky.com/', 4689, 'CN'],
[4, '微博', 'https://weibo.com/', 20, 'CN'],
[5, 'Facebook', 'https://www.facebook.com/', 3, 'USA'],
] as $row) {
$ins->execute($row);
}
$handleGetSite = static function (PDO $db, ?string $rawQ): string {
$q = ($rawQ !== null) ? intval($rawQ) : 0;
if (empty($q)) {
return '请选择一个网站';
}
$stmt = $db->prepare('SELECT id, name, url, alexa, country FROM Websites WHERE id = :id');
$stmt->bindValue(':id', $q, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$out = "<table border='1'><tr><th>ID</th><th>网站名</th><th>网站 URL</th><th>Alexa 排名</th><th>国家</th></tr>";
foreach ($rows as $r) {
$out .= '<tr>'
. '<td>' . htmlspecialchars((string) $r['id'], ENT_QUOTES, 'UTF-8') . '</td>'
. '<td>' . htmlspecialchars((string) $r['name'], ENT_QUOTES, 'UTF-8') . '</td>'
. '<td>' . htmlspecialchars((string) $r['url'], ENT_QUOTES, 'UTF-8') . '</td>'
. '<td>' . htmlspecialchars((string) $r['alexa'], ENT_QUOTES, 'UTF-8') . '</td>'
. '<td>' . htmlspecialchars((string) $r['country'], ENT_QUOTES, 'UTF-8') . '</td>'
. '</tr>';
}
$out .= '</table>';
return $out;
};
// Case 1: Lookup by valid integer ID returns formatted HTML table
$html1 = $handleGetSite($pdo, '1');
$html4 = $handleGetSite($pdo, '4');
add_case(
$cases,
'websites_table_lookup_by_id',
[
'id1_has_google' => true,
'id1_tr_count' => 2,
'id4_has_weibo' => true,
'id4_has_cn' => true,
],
[
'id1_has_google' => str_contains($html1, '<td>Google</td>'),
'id1_tr_count' => substr_count($html1, '<tr>'),
'id4_has_weibo' => str_contains($html4, '<td>微博</td>'),
'id4_has_cn' => str_contains($html4, '<td>CN</td>'),
]
);
// Case 2: Empty or non-numeric ID returns prompt without querying DB
add_case(
$cases,
'empty_or_invalid_id_guard',
[
'empty' => '请选择一个网站',
'zero' => '请选择一个网站',
'alpha' => '请选择一个网站',
],
[
'empty' => $handleGetSite($pdo, ''),
'zero' => $handleGetSite($pdo, '0'),
'alpha' => $handleGetSite($pdo, 'abc'),
]
);
// Case 3: Parameterized query & intval() block SQL injection payload
$sqliHtml = $handleGetSite($pdo, '1 OR 1=1');
$strictStmt = $pdo->prepare('SELECT COUNT(*) FROM Websites WHERE id = :id');
$strictStmt->execute([':id' => '1 OR 1=1']);
$strictCount = (int) $strictStmt->fetchColumn();
add_case(
$cases,
'parameterized_query_blocks_sqli',
['intval_handler_data_rows' => 1, 'strict_string_param_rows' => 0],
['intval_handler_data_rows' => substr_count($sqliHtml, '<tr>') - 1, 'strict_string_param_rows' => $strictCount]
);
// Case 4: Non-existent numeric ID produces header-only table (0 data rows)
$missingHtml = $handleGetSite($pdo, '999');
add_case(
$cases,
'missing_id_empty_table_rows',
['header_only_tr_count' => 1, 'td_count' => 0],
['header_only_tr_count' => substr_count($missingHtml, '<tr>'), 'td_count' => substr_count($missingHtml, '<td>')]
);
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、PHP 8.4 本地 CLI 环境。
入口文件:projects/php-ajax-database-lab/explore-ajax-database.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ajax-database.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1222.zip
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip