Posted By

dmertl on 12/18/13


Tagged

http php request


Versions (?)

PHP - Log request data


 / Published in: PHP
 

Write request data to a log file

  1. <?php
  2.  
  3. class Logger {
  4.  
  5. protected $fh;
  6.  
  7. public function __construct() {
  8. $this->fh = fopen('log.txt', 'a+');
  9. }
  10.  
  11. public function log($msg) {
  12. if(!$this->fh) {
  13. throw new Exception('Unable to open log file for writing');
  14. }
  15. if(fwrite($this->fh, $msg . "\n") === false) {
  16. throw new Exception('Unable to write to log file.');
  17. }
  18. }
  19.  
  20. public function __destruct() {
  21. fclose($this->fh);
  22. }
  23. }
  24.  
  25. $logger = new Logger();
  26. $logger->log(date('m-d-Y H:i:s') . ' ' . $_SERVER['REMOTE_ADDR']);
  27. $logger->log('$_POST: ' . print_r($_POST, true));
  28. $logger->log('$_GET: ' . print_r($_GET, true));
  29. $logger->log('$_FILES: ' . print_r($_FILES, true));

Report this snippet  

You need to login to post a comment.