asvela asvela - 2 months ago 7
PHP Question

How to make php open a html-file, go two lines up, then write there and close?

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

Answer

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

Comments