tags:

views:

2232

answers:

3

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

Thanks

+4  A: 

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

mabwi
A: 

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

Mario
A: 

I would recommend to keep all data in database, and create .html file using .php script to read from that database...

dr.mr.