Skip to content

Instantly share code, notes, and snippets.

@kingkool68
Created October 23, 2023 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingkool68/e4f217e4fbe7c6463cd6d67de73104f5 to your computer and use it in GitHub Desktop.
Save kingkool68/e4f217e4fbe7c6463cd6d67de73104f5 to your computer and use it in GitHub Desktop.
Log $_GET or $_POST data to a CSV file. Example: index.php?bar=bar&foo=foo&baz=baz&bad-data=dont-save would append the following to the data.csv file: 1698022463,foo,bar,baz
<?php
// Merge the expected data with the data recieved
$expected_data = array(
'timestamp' => '',
'foo' => '',
'bar' => '',
'baz' => '',
);
$data = array_merge( $expected_data, $_REQUEST );
// Filter out any extra keys that might be passed in
$data = array_intersect_key( $data, $expected_data );
// If the data is empty then bail
if ( empty( $data ) ) {
return;
}
// Set the timestamp value to the current time
$data['timestamp'] = time();
// Append the data to a CSV file
$handle = fopen( 'data.csv', 'a' );
fputcsv( $handle, $data );
fclose( $handle );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment