1

我正在制作一个小 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 和日期/时间</table> 我已经有了 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); 
?>
4

2 回答 2

4

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

<?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);
?>    

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

于 2009-01-31T21:05:21.370 回答
0

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

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

2)如果你想改变布局,它总是可以通过编辑 .php 脚本

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

于 2009-02-06T18:42:47.970 回答