I have a text file while contains a variable amount of lines and each line contains 3 things, an IP, browser info and a date. Basically it's a visitor log that contains these things.
I want to take a line, get each seperate part and replace rows in a html documents table. As of now I'm only able to get the first line from the file.
When I tried to just print something in the while loop that goes through each line to see if it actually goes through it multiple times it doesn't. It only echos the one time, it goes one lap and reads one line and then stops.
Text file contains for example:
- 2019/02/12 02:32:56--Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36--::1
- 2019/02/12 02:32:58--Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36--::1
- 2019/02/12 02:33:02--Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36--::1
Date, browser info and IP.
<?php
// file path
$path = './visitors.txt';
$html = file_get_contents("log.html");
$visitor_log = fopen($path, 'r');
if(flock($visitor_log, LOCK_SH)){
// Loop genom alla rader i visitors.txt
while (($line = fgets($visitor_log)) !== false) {
$html_pieces = explode("<!--==xxx==-->", $html, 3);
$string_split = explode("--", $line, 3);
$html = str_replace("---date---", $string_split[0], $html);
$html = str_replace("---browser---", $string_split[1], $html);
$html = str_replace("---ip---", $string_split[2], $html);
}
fclose($visitor_log);
}else{
die("Error! Couldn't read from file.");
}
echo $html;
?>
I have no idea why the loop doesn't go through the entire file. Is there a way to just echo every line to see if it can actually read all lines?
EDIT: I just tried
echo $html;
in the while loop and it prints out the first line three times so I believe it goes through all three lines but it doesn't get the new data. Does it have something to do with:
$html = file_get_contents("log.html");
not getting updated?
EDIT: html code for the table
<table cellspacing="0" cellpadding="10" border="1" width="100%">
<thead>
<tr>
<th align="left">Dare</th>
<th align="left">Browser</th>
<th align="left">IP</th>
</tr>
</thead>
<tbody>
<!--==xxx==-->
<tr>
<td align="left">---date---</td>
<td align="left">---browser---</td>
<td align="left">---ip---</td>
</tr>
<!--==xxx==-->
</tbody>
</table>