input.php:輸入表單(標題、關鍵字、內容)result.php:接收資料、過濾、產生靜態 HTML 檔、更新計數器<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>發佈文章</title>
</head>
<body>
<form method="POST" action="result.php" enctype="multipart/form-data">
<table align="center">
<tr><td>標題:</td><td><input type="text" name="nickname" size="50"></td></tr>
<tr><td>關鍵字:</td><td><input type="text" name="key" size="50"></td></tr>
<tr><td>正文:</td><td><textarea name="note" cols="100" rows="20"></textarea></td></tr>
<tr><td colspan="2" align="center">
<input type="submit" value="確定"> <input type="reset" value="重寫">
</td></tr>
</table>
</form>
</body>
</html>
<?php
header("Content-Type: text/html; charset=utf-8");
$nickname = $_POST['nickname'] ?? '';
$key = $_POST['key'] ?? '';
$note = $_POST['note'] ?? '';
if (!$nickname || !$key || !$note) {
die('欄位不得為空');
}
// 安全過濾
$note = htmlspecialchars($note, ENT_QUOTES, 'UTF-8');
$note = nl2br($note); // 換行轉
// 讀取/更新計數器
$counterFile = 'count.txt';
$count = file_exists($counterFile) ? (int)file_get_contents($counterFile) : 0;
$count++;
file_put_contents($counterFile, $count);
// 產生靜態 HTML 檔名
$filename = $count . '.html';
$filepath = __DIR__ . '/articles/' . $filename;
// 確保目錄存在
if (!is_dir(__DIR__ . '/articles')) {
mkdir(__DIR__ . '/articles', 0755, true);
}
// 組裝 HTML 內容
$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{$nickname}</title>
<meta name="keywords" content="{$key}">
<meta name="author" content="{$nickname}">
</head>
<body>
<table width="700" align="center" border="0" cellspacing="1" cellpadding="0" bgcolor="#006633">
<tr bgcolor="#E0F0E8"><td height="40">{$nickname}</td></tr>
<tr bgcolor="#E0F0E8" valign="top"><td height="338">{$note}</td></tr>
</table>
</body>
</html>
HTML;
file_put_contents($filepath, $html);
echo "<center><b><font color=#FF99FF>遞交成功,檔案:{$filename}</font></b></center>";
echo '<center><a href="input.php">返回</a></center>';
?>
htmlspecialchars() + nl2br() 替代手工 str_replace__DIR__ 避免相對路徑問題flock() 或改用資料庫原始來源:www.111cn.net