Dec
18
2011Logging PHP $_POST, $_FILES data for Debugging
PHP Development / Testing & Debugging / Webmaster Tasks
Posted by Ali Qureshi on December 18th, 2011
/
0
comments
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:
Recent Comments
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)