0

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>
Fresh Java
  • 107
  • 1
  • 10
  • Possible duplicate of [Read a plain text file with php](https://stackoverflow.com/questions/4103287/read-a-plain-text-file-with-php) – manuerumx Feb 13 '19 at 10:40

3 Answers3

1

The problem is with how you are adding data to your $html:

$html = str_replace("---date---", $string_split[0], $html); 

This will replace every instance of ---date--- in your $html string with the date of the line currently being processed. If $html is a single row template, that means it is converted into a row of data for the first line. That will work fine.

But for the next line, the substitution strings ---date--- etc do not appear in $html any more - $html now includes your line 1 data. So the str_replace() doesn't find anything to replace, and won't actually do anything, and you'll never see anything except the first line.

The best solution would be to keep your template and the generated results separate:

$html = file_get_contents("log.html");

// Split up your html into header, data row, and the bottom
$html_pieces = explode("<!--==xxx==-->", $html, 3); 

// Show the table header
echo $html_pieces[0];

$path = './visitors.txt';
$visitor_log = fopen($path, 'r');

while (($line = fgets($visitor_log)) !== false) {
    $string_split = explode("--", $line, 3); 

    // Generate a new line of $output from the data row of your $html_pieces
    $output = str_replace("---date---", $string_split[0], $html_pieces[1]); 
    $output = str_replace("---browser---", $string_split[1], $output); 
    $output = str_replace("---ip---", $string_split[2], $output); 

    // And display, line-by-line
    echo $output;
}

// All done, finish off your table
echo $html_pieces[2];
Don't Panic
  • 13,965
  • 5
  • 32
  • 51
0

I think you ought to be able to read the text file like this perhaps. Using file to open the text file puts the contents into an array which is then easy to loop through and using list combined with explode makes it easy to generate variables. I couldn't really determine what the code was actually dong above as there seems to be no output and couldn't understand where log.html fitted in but hopefully this might help. ( not tested )

$textfile = './visitors.txt';
$lines = file( $textfile, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES );

foreach( $lines as $line ){
    list( $date, $useragent, $ip )=explode( '--', $line );
    echo $date,$useragent,$ip,'<br />';
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

I just solved it!

The problem was

$html = file_get_contents("log.html");

and

echo $html;

Both lines have to be in the while loop.

Fresh Java
  • 107
  • 1
  • 10
  • 1
    Great you solved it. If `log.html` is not changing, there's no need to re-read it for every line of data though, you're just hammering your disk for nothing. See my answer for a way to avoid that. – Don't Panic Feb 13 '19 at 12:26
  • @Don'tPanic I just realised that I am reading the table table head once for every line, is there a way to only write out table head once on the top and then print out every row? I added my html code above. – Fresh Java Feb 13 '19 at 13:06
  • 1
    So your `log.html` includes a set of headings, and a blank template row? Can you add it to your question? Ah, you just did :-) Let me have a look. – Don't Panic Feb 13 '19 at 13:08
  • 1
    I've updated my answer. You were already partway there with your `$html_pieces `. – Don't Panic Feb 13 '19 at 13:13
  • It works but my output is all messed up, it doesn't put it in the table and the header doesn't show check image: https://gyazo.com/05a9a5d15795ab9d8f85985b18aed5b5 – Fresh Java Feb 13 '19 at 13:20
  • 1
    I had a typo in the first version of my code, and edited it a few times - are you using the latest? If yes, what does the generated source you are seeing look like? That should give a clue about what is wrong. – Don't Panic Feb 13 '19 at 13:22
  • You're a PHP genie, I tried your code and it works perfectly. – Fresh Java Feb 13 '19 at 13:28
  • 1
    :-) Glad it helped. – Don't Panic Feb 13 '19 at 13:28