PHP logging system

Asked

Viewed 49 times

0

I wanted to record everything that happens on my site, from logins, logging (Insert), updating and deletion.
Like that:

[14-05-2016 17:53] User Andre logged in with IP: 111.111.111.111
[14-05-2016 17:54] User Andre eliminated client with ID 2
[14-05-2016 17:55] User Andre created client with ID 3

I created a script in php to write the records to a file log.log
Contents of the file regista.php

<?php
date_default_timezone_set('Europe/Lisbon'); $cache_new = date("\[d-m-Y H:i\] ");
header('Content-Type: text/html; charset=utf-8');

$cache_new .= $_GET['q']; 

$file = "C:\\xampp\\htdocs\\pap\\registos\\log.log"; 
$cache_new .= "\n";
$handle = fopen($file, "r+") or die ();
$len = strlen($cache_new);
$final_len = filesize($file) + $len;
$cache_old = fread($handle, $len);
rewind($handle);
$i = 1;
while (ftell($handle) < $final_len) {
  fwrite($handle, $cache_new);
  $cache_new = $cache_old;
  $cache_old = fread($handle, $len);
  fseek($handle, $i * $len);
  $i++;
}
?>

If I go to http://meusite/registo/regista.php?q=Utilizador fez login it will register in the file.
But I wanted automatic, for example the file eliminar.php, after the script delete the database client will log.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://omeusite/registos/regista.php?q=Utilizador fez login");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec($ch);
curl_close($ch);
echo $output;

By putting this into eliminar.php, after executing the query in the database the page is extremely slow and does not record the entry.
What I did wrong?

  • Making an external request is not the appropriate way. It would be better to write to DB directly, or in an intermediate file. By the way, curl in most cases where it is used, is not the best solution.

  • @Bacco knows of some alternative without resorting to database?

  • What would be the problem of the database? remember that you can use a different one than the one used in the application, and even external, if it is for audit. In the worst case, if you prefer, you can leave that part of Curl after the page is sent to the client, and flush the buffer. will continue with the delay of the request, but the user will not be affected.

  • And need to test this your separate url, because there may be some error in the address and so it takes and does not register.

  • I’m switching to the database for records.

  • Thanks anyway @Bacco

  • Anyway, test the Url separately, and once you get it to work, play at the bottom of the page, who knows change something, at least to locate the problem. DB still seems more practical for your case.

Show 2 more comments
No answers

Browser other questions tagged

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