php - 我如何将 php 文本放在 html 之上
问题描述
我有一个简单的聊天系统,我想做的是将新消息放在 html 之上。我的脚本将新消息放在底部,我想更改它。
<?php
session_start();
if(isset($_SESSION['name'])){
$text = $_POST['text'];
$text_message = "<div class='msgln'><span class='chat-time'>".date("m.d.y")."</span><br> <b
class='user-name'>".$_SESSION['name']."</b> ".stripslashes(htmlspecialchars($text))."<p></div>";
file_put_contents("../log.html", $text_message, FILE_APPEND | LOCK_EX);
}
?>
该脚本在干净的 log.html 文件上打印文本
解决方案
您正在使用file_put_contents()
函数附加文件内容,因此新添加到文件中的内容最终将到达底部。
我会为您的问题建议一个解决方法。首先将您的log.html
文件内容保存到一个变量中,将新添加的消息覆盖到文件中(覆盖整个文件),最后将您保存的日志文件内容附加到文件中。这样,您可以使新内容始终显示在文件顶部。
<?php
session_start();
if(isset($_SESSION['name'])){
$text = $_POST['text'];
$file_content = file_get_contents("../log.html");
$text_message = "<div class='msgln'><span class='chat-time'>".date("m.d.y")."</span><br> <b
class='user-name'>".$_SESSION['name']."</b> ".stripslashes(htmlspecialchars($text))."<p></div>";
file_put_contents("../log.html", $text_message);
file_put_contents("../log.html", $file_content, FILE_APPEND | LOCK_EX);
}
?>
推荐阅读
- ios - 使用 Docker 自动化 iOS 测试
- sql-server - SSIS/SQL:将在多个相关临时表中准备的数据移动到“真实”表而不覆盖现有数据(附加)
- git - 当检查失败时,危险不会发出警告
- html - Angular 项目的 Internet Explorer 不支持 input type="date"
- vb.net - VB.NET 有像 C# 那样的模式匹配吗?
- python - 在 Python 中实现 Miller-Rabin 复合性时遇到问题
- selenium - 如何在不使用 perl 在 selenium 中使用睡眠的情况下等待动画完成?
- mysql - 用户'root'@'localhost'的访问被拒绝(使用密码:是) - 没有权限?MySQL 工作台 ubuntu 20.04
- reactjs - ReactNative - 在 FBSDK 中呈现 userInfo 时使用数组错误呈现一组子项?
- python - 您如何确定扩展将使用哪些版本的 VS Code?