2025.11.02

日志以及文章完善了数据库存储
接下来就是完成上传数据库的前端与后端的融合了

**署名**
SkimrMe

 # 修改部分
 Changes to be committed:
	modified:   public/blog/index.php
	modified:   public/blog/logs.php
	new file:   public/blog/post/index.php
	new file:   public/blog/post/s/index.php
	deleted:    public/blog/read_file.php
	modified:   public/index.php
	deleted:    public/request/posts.php
	deleted:    src/views/index.php
	deleted:    src/views/post.php
	deleted:    src/views/posts.php
This commit is contained in:
2025-11-02 14:54:04 +08:00
parent 7727a97e4a
commit 0c70b66966
10 changed files with 162 additions and 162 deletions

View File

@@ -0,0 +1,46 @@
<?php
// 设定数据库路径
$db_path = '/var/www/owp/open-ww3-project-ww3-tw/databases/sqlite/owp.db';
try {
// 连接数据库
$db = new SQLite3($db_path);
// 连接成功输出连接成功
echo "<!--数据库连接成功-->";
// 如果连接成功,但是内部状态有问题
if ($db->lastErrorCode() !==0) {
// 依旧显示为连接失败
die("数据库连接失败");
}
// 内容区
// 执行sql命令 查询表单
$select_id_date_title_from_posts /*查询posts表单中的id date title id从小到大排列*/ = $db->query('SELECT id, date, title FROM posts ORDER BY id ASC'); // 执行查询posts表单中的id date title id从小到大排列的命令
// 循环 写入
echo "<center><b><h2>全部文章</h2></b></center>";
echo "文章如下↓";
echo "<br>";
echo "<br>";
while ($row = $select_id_date_title_from_posts->fetchArray(SQLITE3_ASSOC)) {
echo "文章id: " . $row['id'] . "<br>";
echo $row['date'] . "&nbsp;&nbsp;&nbsp;" . "文章标题: " . $row['title'] . "<br>";
echo "<a href='https://ww3.tw/blog/post/s/?id=" . $row['id'] . "'>页面跳转</a><br><br>";
}
// 关闭数据库连接
$db->close();
// 捕获php报错
} catch (Exception $e) {
// 依旧显示为连接失败
die("数据库连接失败");
// 关闭数据库连接
$db->close();
}
?>

View File

@@ -0,0 +1,110 @@
<?php
/**
* ==========================================================
* 文章内容显示程序 (post.php)
* 功能: 根据 URL 中的 ID (例如: ?id=1) 从 SQLite 数据库中查询并显示文章内容。
* 安全性: 采用预处理语句防止 SQL 注入,并对 ID 进行了严格的类型和范围检查。
* ==========================================================
*/
// 设置最大可接受的文章 ID 阈值(文章数预估)
const MAX_ARTICLE_ID = 100000;
// 1. 获取 URL 中的 ID 参数
$post_id = $_GET['id'] ?? null;
// ==========================================================
// 🚨 您的 SQLite 数据库文件路径 🚨
// 已更新为您的指定路径:
$db_path = '/var/www/owp/open-ww3-project-ww3-tw/databases/sqlite/owp.db';
// ==========================================================
/**
* 2. 严格的输入验证 (健壮性检查)
* 拦截所有非数字、无效范围或为空的输入
*/
if (
empty($post_id) ||
!is_numeric($post_id) ||
(int)$post_id <= 0 ||
(int)$post_id > MAX_ARTICLE_ID
) {
http_response_code(400); // HTTP 400 Bad Request
$error_msg = "错误:文章 ID 格式不正确。ID 必须是 1 到 " . MAX_ARTICLE_ID . " 之间的整数。";
die($error_msg);
}
// 通过检查后,将 ID 强制转换为整数
$post_id = (int)$post_id;
try {
/**
* 3. 连接 SQLite 数据库
* 注意:如果 PHP 运行用户没有读取此路径文件的权限,这里会失败。
*/
$db = new SQLite3($db_path);
/**
* 4. 准备 SQL 查询语句 (核心安全防线:预处理语句)
* 使用占位符 :id 代替用户输入。
*/
$sql = 'SELECT title, content FROM posts WHERE id = :id';
// 准备语句
$stmt = $db->prepare($sql);
// 绑定参数:将 :id 替换为 $post_id 的值,并明确指定它是一个整数类型
$stmt->bindValue(':id', $post_id, SQLITE3_INTEGER);
/**
* 5. 执行查询并获取结果
*/
$result = $stmt->execute();
// 从结果集中获取一行数据
$row = $result->fetchArray(SQLITE3_ASSOC);
/**
* 6. 显示内容或 404 错误
*/
if ($row) {
// 使用 htmlspecialchars() 函数防止 XSS 攻击
$title = $row['title'];
$content = $row['content'];
// --- 页面输出开始 ---
echo "<!DOCTYPE html>";
echo "<html lang='zh-CN'>";
echo "<head><meta charset='UTF-8'><title>{$title}</title></head>";
echo "<body>";
echo "<h1>{$title}</h1>";
// nl2br 用于将换行符转换成 <br>
echo "<article>" . nl2br($content) . "</article>";
echo "</body>";
echo "</html>";
// --- 页面输出结束 ---
} else {
// 文章未找到
http_response_code(404); // 404 Not Found
echo "<h1>404 Not Found</h1>";
echo "<p>抱歉ID 为 {$post_id} 的文章不存在。</p>";
}
/**
* 7. 清理资源
*/
$db->close();
//$stmt->close();
} catch (Exception $e) {
// 处理异常
http_response_code(500); // 500 Internal Server Error
error_log("SQLite 错误: " . $e->getMessage());
die("系统错误,请稍后重试。");
}
?>