Logging PHP $_POST, $_FILES data for Debugging

debugging

Debugging is part and parcel of development and especially now applications getting complex with the advent of mobile apps development, developers need to log Form submitted $_POST and $_FILES data. Here’s a useful snippet to log the posted data that you can place in your PHP script to help you identify the problematic part.

Jut create the /logs/ directory along side the file where you include the code:

 
if ( isset($_POST) && is_array($_POST) && count($_POST) > 0 ) {
  $log_dir = dirname( __FILE__ ) . '/logs/';
  $log_name = "posts-" . $_SERVER['REMOTE_ADDR'] . "-" . date("Y-m-d-H") . ".txt";
  $log_entry = gmdate('r') . "\t" . $_SERVER['REQUEST_URI'] . "\r\n" . serialize($_POST) . "\r\n\r\n";
  $fp=fopen( $log_dir . $log_name, 'a' );
  fputs($fp, $log_entry);
  fclose($fp); }
 
if ( isset($_FILES) && is_array($_FILES) && count($_FILES) > 0 ) {
  $log_dir = dirname( __FILE__ ) . '/logs/';
  $log_name = "posts-" . $_SERVER['REMOTE_ADDR'] . "-" . date("Y-m-d-H") . ".txt";
  $log_entry = gmdate('r') . "\t" . $_SERVER['REQUEST_URI'] . "\r\n" . serialize($_FILES) . "\r\n\r\n";
  $fp=fopen( $log_dir . $log_name, 'a' );
  fputs($fp, $log_entry);
  fclose($fp); }

Hope that helps.

Related posts:

  1. how to unzip files on remote server using PHP script
  2. Resolve function.getimagesize: failed to open stream: 403 Forbidden Error
  3. [osCommerce] Resolve osCommerce Admin Panel Product Attributes Page Navigation Problem