如何让php打开一个html文件,向上两行,然后在那里写入并关闭?

发布于 2024-07-12 22:45:26 字数 916 浏览 7 评论 0原文

我正在制作一个小型 php 文件来记录一些 IP 地址。 它将把 ips 和日期/时间写入 html 文件。 html 文件将是一个表格。 所以我想这样做:

<table cellpadding="6" rules="groups"  frame="no">
<thead>
<tr><th>IP</th><th>Date</th><th>Time</th></tr>

</thead>
<tbody>
<tr><td>192.168.0.1</td><td>31. January 2009</td><td>19:21:09</td></tr> 
</tbody>
</table>

所以我需要它来打开文件,在 上面的行中写入 ip 和日期/时间 我已经有了 php 来写我想要的东西,但它写的是底部。

我是新手,我不知道该把什么放在哪里..

这就是我所拥有的:

<?php
$ip = $_SERVER['REMOTE_ADDR']; 
$date = date("j. F Y"); 
$time = date("H:i:s"); 
$file = fopen('./iplogg.html', 'a', 1); 
$text="<tr><td>{$ip}</td><td>{$date}</td><td>{$time}</td></tr> \n"; 
fwrite($file, $text); 
fclose($file); 
?>

I am making a litte php-file to log some ip addresses. It is going to write the ips and date/time into a html-file. The html-file is going to be a table. So I want to mak it like this:

<table cellpadding="6" rules="groups"  frame="no">
<thead>
<tr><th>IP</th><th>Date</th><th>Time</th></tr>

</thead>
<tbody>
<tr><td>192.168.0.1</td><td>31. January 2009</td><td>19:21:09</td></tr> 
</tbody>
</table>

So I need it to open the file, write the ip and date/time on the line above </table>
I already have the php to write what I want, but it writes a the bottom.

I am very newbie, I don't know what to put in where..

This is what I have:

<?php
$ip = $_SERVER['REMOTE_ADDR']; 
$date = date("j. F Y"); 
$time = date("H:i:s"); 
$file = fopen('./iplogg.html', 'a', 1); 
$text="<tr><td>{$ip}</td><td>{$date}</td><td>{$time}</td></tr> \n"; 
fwrite($file, $text); 
fclose($file); 
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌若浮生 2024-07-19 22:45:26

我假设文件中只有一个表。 如果有多个,这会将其添加到每个中。

<?php
$ip = $_SERVER['REMOTE_ADDR']; 
$date = date("j. F Y"); 
$time = date("H:i:s"); 
$text="<tr><td>{$ip}</td><td>{$date}</td><td>{$time}</td></tr> \n"; 

$originalfile = file_get_contents ('./iplogg.html');
$newFile = str_replace('</table>',$text.'</table>',$originalfile);
file_put_contents('./iplogg.html', $newFile);
?>    

编辑将我的建议与您的代码混合

I'm going to assume that there's only a single table in the file. If there's more than one, this will add it to each.

<?php
$ip = $_SERVER['REMOTE_ADDR']; 
$date = date("j. F Y"); 
$time = date("H:i:s"); 
$text="<tr><td>{$ip}</td><td>{$date}</td><td>{$time}</td></tr> \n"; 

$originalfile = file_get_contents ('./iplogg.html');
$newFile = str_replace('</table>',$text.'</table>',$originalfile);
file_put_contents('./iplogg.html', $newFile);
?>    

EDIT Mixed my suggestion with your code

迷途知返 2024-07-19 22:45:26

我建议使用 2 个文件,一个 .log 文件用于存储原始数据,一个 .php 脚本从该 .log 文件读取并生成一个表。 主要原因是:

1)您的 .log 文件将保持小得多

2)如果您想更改布局,总是可以通过编辑 .php 脚本来实现

3)当您的 .log 文件变得巨大时,这可能是不可能的使用 file_get_contents 将其内容存储到字符串中

What I would suggest is using 2 files, one .log file to store the raw data and a .php script that reads from that .log file and generate a table. The main reasons are:

1) your .log file will remain much smaller

2) if you ever want to change the layout, its always possible by editing the .php script

3) when your .log file becomes HUGE, it might not be possible to store its content into a string with file_get_contents

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文