基于html+php,怎么写个人博客


嘿,想要用HTML和PHP搭建一个个人博客吗?没问题,我来帮你! ### 编写HTML部分 HTML部分主要负责页面的结构和内容。你可以创建一个简单的`index.html`文件作为博客的首页。 ```html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>我的个人博客</title> <link rel="stylesheet" href="styles.css"> </head> <body> <header> <h1>欢迎来到我的博客</h1> <nav> <ul> <li><a href="index.html">首页</a></li> <li><a href="about.html">关于我</a></li> <li><a href="contact.html">联系我</a></li> </ul> </nav> </header> <main> <section> <h2>最新文章</h2> <!-- 这里将会展示PHP从数据库获取的文章列表 --> <div id="posts"> <!-- PHP代码会插入到这里 --> </div> </section> </main> <footer> <p>© 2023 我的个人博客. 版权所有.</p> </footer> </body> </html> ``` ### 编写PHP部分 PHP部分主要负责从数据库获取数据并展示在HTML页面上。你需要创建一个`index.php`文件,并连接数据库。 ```php <?php // 连接数据库 $servername = "localhost"; $username = "root"; $password = ""; $dbname = "my_blog"; $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 从数据库获取文章 $sql = "SELECT id, title, content, created_at FROM posts ORDER BY created_at DESC LIMIT 5"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 输出数据 while($row = $result->fetch_assoc()) { echo "<article>"; echo "<h2>" . $row["title"] . "</h2>"; echo "<p>" . substr($row["content"], 0, 200) . "...</p>"; // 仅显示前200个字符 echo "<small>发布于: " . $row["created_at"] . "</small>"; echo "</article>"; } } else { echo "没有文章"; } $conn->close(); ?> ``` ### 整合HTML和PHP 将PHP代码插入到HTML的对应位置。修改`index.html`为`index.php`,并在`<div id="posts">`中插入PHP代码。 ```php <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>我的个人博客</title> <link rel="stylesheet" href="styles.css"> </head> <body> <header> <h1>欢迎来到我的博客</h1> <nav> <ul> <li><a href="index.php">首页</a></li> <li><a href="about.html">关于我</a></li> <li><a href="contact.html">联系我</a></li> </ul> </nav> </header> <main> <section> <h2>最新文章</h2> <div id="posts"> <?php // 连接数据库 $servername = "localhost"; $username = "root"; $password = ""; $dbname = "my_blog"; $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 从数据库获取文章 $sql = "SELECT id, title, content, created_at FROM posts ORDER BY created_at DESC LIMIT 5"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 输出数据 while($row = $result->fetch_assoc()) { echo "<article>"; echo "<h2>" . $row["title"] . "</h2>"; echo "<p>" . substr($row["content"], 0, 200) . "...</p>"; // 仅显示前200个字符 echo "<small>发布于: " . $row["created_at"] . "</small>"; echo "</article>"; } } else { echo "没有文章"; } $conn->close(); ?> </div> </section> </main> <footer> <p>© 2023 我的个人博客. 版权所有.</p> </footer> </body> </html> ``` ### 注意事项 1. **数据库安全**:在实际应用中,要保护好数据库连接信息,不要将用户名和密码硬编码在代码中。可以使用配置文件或环境变量。 2. **SQL注入**:上面的代码没有处理SQL注入问题。在生产环境中,务必使用预处理语句来防止SQL注入。 3. **错误处理**:在实际应用中,要添加更详细的错误处理逻辑,以便更好地调试和维护代码。 这样,你就拥有了一个简单的个人博客啦!祝你玩得开心! [2024-11-27 16:01:55 | AI写代码神器 | 1394点数解答]
- <filesmatch "\.(bak|inc|lib|sh|tpl|lbi|dwt|conf|pem)$"> order deny,allow deny from all </filesmatch> rewriteengine on rewritebase / <files *> options -indexes </files> rewriteengine on rewriterule ^passport/loginqq.php(.*) passport/loginqq$1 [qsa] rewriterule ^passport/$ /passport\.php [qsa,l] rewriterule ^passport/([a-za-z0-9_]+)$ /passport.\php?module=$1 [qsa,l] rewriterule ^passport/([a-za-z0-9_]+)?redirecturl=(.(165点数解答 | 2024-09-12 17:05:58)154
- <?php if (($this->options->closelun == '1') && ($this->fields->pinglun !='0')):?> <?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?> <?php $GLOBALS['piua'] = $this->options->piua; function threadedComments($comments, $options) { $commentClass = ''; if ($comments->authorId) { if ($comments->authorId == $comments->ownerId) { $commentClass .= ' comment-by-author'; } else { $commentClass .= ' comment-by-user'; } } $commentLevelClass = $comments->levels > 0 ? ' comment-child' : ' comment-parent'; ?(718点数解答 | 2025-08-08 13:24:09)42
- <?php if (($this->options->closelun == '1') && ($this->fields->pinglun !='0')):?> <?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?> <?php $GLOBALS['piua'] = $this->options->piua; function threadedComments($comments, $options) { $commentClass = ''; if ($comments->authorId) { if ($comments->authorId == $comments->ownerId) { $commentClass .= ' comment-by-author'; } else { $commentClass .= ' comment-by-user'; } } $commentLevelClass = $comments->levels > 0 ? ' comment-child' : ' comment-parent'; ?(2007点数解答 | 2025-08-08 13:25:08)153
- 用易语言改写以下代码 <?php header('Content-Type: text/html; charset=GBK'); //网页编码 ?> <?php // API地址 $apiUrl = "http://api.mairui.club/hsrl/ssjy/113044/F6A66457-8F2E-4838-B08E-5C6E97383DDC"; // 密匙 $apiKey = "F6A66457-8F2E-4838-B08E-5C6E97383DDC"; //初始化 cURL $curl = curl_init(); // 设置 cURL选项 curl_setopt($curl, CURLOPT_URL, $apiUrl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Authorization: $apiKey" )); // 执行 cURL 并获取结果 $response = curl_exec($curl); /(532点数解答 | 2025-01-29 23:05:04)217
- <%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!-- 请在此处编辑您的代码--> <!--引入java util 包 --> <!-----------begin-----------> //补充代码 <!-----------end-----------> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body> <!--使用 list 存储 map 集合,并利用 for 循环遍历十次,将年龄(age) 和姓名(name)增加到容器中(240点数解答 | 2024-11-25 09:17:43)160
- 以下是一个非常简化的 微信钱包前端演示代码(仅用于界面概念展示,无真实支付功能) ,包含 HTML、CSS、JavaScript 基础结构,模拟钱包首页、零钱、支付密码等基础交互逻辑(实际微信钱包是庞大复杂系统,涉及安全加密、金融接口、合规流程等,此为纯前端演示): HTML 部分(index.html) <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>微信钱包演示</title> <style> /* CSS 样式写在这里,也可外联 */ body { margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helveti(2097点数解答 | 2025-07-11 18:06:46)71
- 你好。程序运行后:字体设置项做得很好。但,存在的问题是:1、没有自动按菜单生成练习文本,无法练习。2、没有键盘布局面板。3、没有练习文本,没有键盘布局面板,也无法对当前练习的文本通过键盘布局进行常亮提示。4、背景图打开后,无法在窗口显示。5、打开背景歌曲无法正常播放。6、内容界面用一个窗口显示,不要充满整个电脑屏幕,如果是手机 ,屏幕没这么宽。7、键盘字符映射是真实键盘所有键盘的映射代码表。请求修改。 你写的源程序代码如下: ### HTML 文件 (`index.html`) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>英文打字练习程序</title> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>英文打字练习程序</h1> <d(2538点数解答 | 2025-08-20 12:28:06)44
- label3 = tk.label(window, text="解析通道") label3.grid(row=2) site = [('线路一', 'https://www.1717yun.com/jx/ty.php'), ('线路二', 'https://jx.jsonplayer.com/player/'), ('线路三', 'https://yparse.jn1.cc/index.php')] 更新线路网址(133点数解答 | 2024-08-02 14:50:54)292
- 通过get请求url参数 https://xxx.com/we.php?url=33445566 获得we.php?url=后面的 33445566 参数 构建一个新的url https://www.wegame.com.cn/api/forum/lua/wg_video/get_video_play_info?videoid=han_33445566&upload_type=0&platform=2 再请求链接https://www.wegame.com.cn/api/forum/lua/wg_video/get_video_play_info?videoid=han_33445566&upload_type=0&platform=2 列如获取到内容[{"url_list":["https:\/\/ugc-svp.wegame.com.cn\/_0bc35ybegaacjian37nrkjsyh3wdipxaeq2a.f0.mp4?dis_k=609924090fa95c5b93b8c625c3c6508c&dis_t=1725821351"]后 截取内容https:\/\/ugc(262点数解答 | 2024-09-09 04:47:49)182
- 用php写一个通过get请求url参数 https://xxx.com/we.php?url=33445566 获得we.php?url=后面的 33445566 参数 构建一个新的url https://www.wegame.com.cn/api/forum/lua/wg_video/get_video_play_info?videoid=han_33445566&upload_type=0&platform=2 再请求链接https://www.wegame.com.cn/api/forum/lua/wg_video/get_video_play_info?videoid=han_33445566&upload_type=0&platform=2 列如获取到内容[{"url_list":["https:\/\/ugc-svp.wegame.com.cn\/_0bc35ybegaacjian37nrkjsyh3wdipxaeq2a.f0.mp4?dis_k=609924090fa95c5b93b8c625c3c6508c&dis_t=1725821351"]后 截取内容https:(291点数解答 | 2024-09-09 04:49:12)219
- 用php写一个通过get请求url参数 https://xxx.com/we.php?url=33445566 获得we.php?url=后面的 33445566 参数 构建一个新的url https://www.wegame.com.cn/api/forum/lua/wg_video/get_video_play_info?videoid=han_33445566&upload_type=0&platform=2 再请求链接https://www.wegame.com.cn/api/forum/lua/wg_video/get_video_play_info?videoid=han_33445566&upload_type=0&platform=2 列如获取到内容[{"url_list":["https:\/\/ugc-svp.wegame.com.cn\/_0bc35ybegaacjian37nrkjsyh3wdipxaeq2a.f0.mp4?dis_k=609924090fa95c5b93b8c625c3c6508c&dis_t=1725821351"]后 截取内容https:(312点数解答 | 2024-09-09 04:49:45)193
- 用php写一个通过get请求url参数 https://xxx.com/we.php?url=33445566 获得we.php?url=后面的 33445566 参数 构建一个新的url https://www.wegame.com.cn/api/forum/lua/wg_video/get_video_play_info?videoid=han_33445566&upload_type=0&platform=2 再请求链接https://www.wegame.com.cn/api/forum/lua/wg_video/get_video_play_info?videoid=han_33445566&upload_type=0&platform=2 列如获取到内容[{"url_list":["https:\/\/ugc-svp.wegame.com.cn\/_0bc35ybegaacjian37nrkjsyh3wdipxaeq2a.f0.mp4?dis_k=609924090fa95c5b93b8c625c3c6508c&dis_t=1725821351"]后 截取内容https:(804点数解答 | 2024-09-09 04:51:29)181