Data overwritten when logging file accesses

Asked

Viewed 215 times

3

I have a problem that I think is easy to solve, but I don’t know how to solve.

Below is a script to record who accessed the page. It worked great, but it overrides one access on top of the other, and I need every IP you accessed recorded, one underneath the other.

Besides I don’t know how to set the schedule, it’s showing up with 5 hours more.

<?php
   /* se o arquivo não existir, será criado, dê
      permissão 777 na pasta onde ele será criado */
   $arquivo = "includes/spy.txt"; // arquivo que gravará os dados
   $data = date("d/m/Y H:i:s"); // Data do acesso
   $ip = $_SERVER['REMOTE_ADDR']; // IP de acesso
   $host = getHostByAddr($ip); // Host de acesso
   $pagina = $_SERVER['PHP_SELF']; // Página de acesso
   $browser = $_SERVER['HTTP_USER_AGENT']; // Browser de acesso
   $fp = fopen($arquivo, "w+");
   fwrite( $fp,
      "Data: $data | IP: $ip | host: $host | Pagina Acessada: $pagina | Navegador: $browser"
   );
   fclose($fp);
?>

2 answers

6

Change the way of fopen for a (append):

$fp = fopen($arquivo, 'a+');

see the difference between the flags of fopen in the PHP manual (highlighted the key points):

w+ Opens for reading and writing; puts the file pointer at the beginning of the file and reduces the file length to zero. If the file does not exist, try to create it.

a+ Opens for reading and writing; puts the file pointer at the end of the file. If the file does not exist, try to create it.


As to the date, the problem is probably the Timezone server. The line below should resolve (use before the date):

date_default_timezone_set('America/Sao_Paulo'); // Europe/Lisbon
  • My friend, thank you very much! It worked perfectly!

  • 2

    Hi Diego. Cool, if this answer solved your problem, you can mark it as "accept" by clicking on the icon below the answer score. More details on http://meta.pt.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-reply

0

    <?php
       /* se o arquivo não existir, será criado, dê
          permissão 777 na pasta onde ele será criado */
       date_default_timezone_set('America/Sao_Paulo');
       $arquivo = "includes/spy.txt"; // arquivo que gravará os dados
       $data = date("d/m/Y H:i:s"); // Data do acesso
       $ip = $_SERVER['REMOTE_ADDR']; // IP de acesso
       $host = getHostByAddr($ip); // Host de acesso
       $pagina = $_SERVER['PHP_SELF']; // Página de acesso
       $browser = $_SERVER['HTTP_USER_AGENT']; // Browser de acesso
       $fp = fopen($arquivo, "a+");
       fwrite( $fp,
          "Data: $data | IP: $ip | host: $host | Pagina Acessada: $pagina | Navegador: $browser"."\r\n"
       );
       fclose($fp);
    ?>

OR

<?php
   /* se o arquivo não existir, será criado, dê
      permissão 777 na pasta onde ele será criado */
   date_default_timezone_set('America/Sao_Paulo');
   $arquivo = "includes/spy.txt"; // arquivo que gravará os dados
   $data = date("d/m/Y H:i:s"); // Data do acesso
   $ip = $_SERVER['REMOTE_ADDR']; // IP de acesso
   $host = getHostByAddr($ip); // Host de acesso
   $pagina = $_SERVER['PHP_SELF']; // Página de acesso
   $browser = $_SERVER['HTTP_USER_AGENT']; // Browser de acesso
   $fp = fopen($arquivo, "a+");
   fwrite( $fp,
      "Data: $data | IP: $ip | host: $host | Pagina Acessada: $pagina | Navegador: $browser"."\r\n"."\r\n"
   );
   fclose($fp);
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.