There is a script that writes information for each visitor to my site in the base.php file as shown in the screenshot: http://prntscr.com/6qlc22

<? $ip=getenv("REMOTE_ADDR"); $date=date("d MY, H:i:s"); $agent=getenv("HTTP_USER_AGENT"); $str=(" Data - $date Ip - $ip Browser - $agent -------"); $log = fopen("base.php","a+"); fwrite($log,"\n $str \n" ); fclose($log); ?> 
  • Place here that part of the script that is responsible for writing data to a file so that the community can correctly answer your question, and not do fortune telling on the coffee grounds. - VenZell
  • <? $ ip = getenv ("REMOTE_ADDR"); $ date = date ("d MY, H: i: s"); $ agent = getenv ("HTTP_USER_AGENT"); $ str = ("Data - $ date Ip - $ ip -------"); $ log = fopen ("base.php", "a +"); fwrite ($ log, "\ n $ str \ n"); fclose ($ log); ?> - Shtyben Viacheslav

3 answers 3

The script writes the data correctly.

Most likely, they are displayed incorrectly. For line wraps to be like in a file, you need to wrap the output of your logs in the <pre> .


Update: now I understand what your problem is. You are trying to open a file in a browser by accessing it directly and not seeing the line breaks you have placed. All this happens because you save data to a file with *.php extension.

Change the extension of your file, for example, to .txt and the problem will be solved.

After that, update your code that writes data to a file in order to save data to a file with a new extension:

 <? $ip=getenv("REMOTE_ADDR"); $date=date("d MY, H:i:s"); $agent=getenv("HTTP_USER_AGENT"); $str=(" Data - $date Ip - $ip Browser - $agent -------"); $log = fopen("base.txt","a+"); // Изменения в этой строчке fwrite($log,"\n $str \n" ); fclose($log); ?> 

Accordingly, now to view the logs you will need to refer to the base.txt file.

  • What will it look like in a script? - Shtyben Viacheslav
  • I have another question: what is your script for displaying data on the screen? - VenZell
  • I posted it in the forum thread prntscr.com/6qm42h - Shtyben Viacheslav
  • Now the browser report looks like this: prntscr.com/6qm4xa - Shtyben Viacheslav
  • The script rewrote and looks like this prntscr.com/6qm5ky . Due to the number of points leveled report. - Shtyben Viacheslav

With this formation of the file, each entry still comes with a new line). Apparently the author is viewing the file in Windows, not Linux. The fact is that in Windows, the standard of line feed is \r\n , while in Linux it’s just \n .

When viewing through a notebook, each entry is displayed on a new line, change the code to this one:

 $str = (" Data - $date\r\n Ip - $ip\r\n Browser - $agent\t\n -------"); $log = fopen("base.txt", "a+"); fwrite($log, "\r\n" . $str . "\r\n"); 

    When output, you can use the banal nl2br .

    I would also pay attention to http://php.net/manual/ru/function.stream-set-write-buffer.php

    UPD

    If you output via file_get_contents , for example, then it might look like this:

     $file = file_get_contents("/base.php"); echo nl2br($file);