PHP Script to Log the Raw Data of Post

PHP script to log the raw data of a POST

You can see the content of any data sent with the post verb (not for production) with:

print_r($_POST);

If you want to see data from the verbs get, post, cookie:

print_r($_REQUEST);

If you need to log, since the client is very limited - try outputting the post data to a file:

file_put_contents("post.log", print_r($_POST, true));
file_put_contents("gpc.log", print_r($_REQUEST, true));

How can I log the full body of a POST request to a file for debugging?

PHP only populates $_POST, when the request Content-Type was either application/x-www-form-urlencoded or multipart/form-data. If you get send anything else, then you have to use php://input to read the raw POST body, and parse it yourself.

The example JSON you have shown appears to be valid - so if json_decode does not give you the expected result here, then your input data was probably not what you expected it to be in the first place.

A basic debugging mistake here is that you are only looking at the final result of multiple “compound” operations. Log what file_get_contents('php://input') returned first of all, to see if that actually is what you expected it to be. If it wasn’t to begin with, then looking only at what json_decode returned, isn’t that helpful.

How to get body of a POST in php?

To access the entity body of a POST or PUT request (or any other HTTP method):

$entityBody = file_get_contents('php://input');

Also, the STDIN constant is an already-open stream to php://input, so you can alternatively do:

$entityBody = stream_get_contents(STDIN);

From the PHP manual entry on I/O streamsdocs:

php://input is a read-only stream that allows you to read raw data
from the request body. In the case of POST requests, it is preferable
to use php://input instead of $HTTP_RAW_POST_DATA as it does not
depend on special php.ini directives. Moreover, for those cases where
$HTTP_RAW_POST_DATA is not populated by default, it is a potentially
less memory intensive alternative to activating
always_populate_raw_post_data. php://input is not available with
enctype="multipart/form-data".

Specifically you'll want to note that the php://input stream, regardless of how you access it in a web SAPI, is not seekable. This means that it can only be read once. If you're working in an environment where large HTTP entity bodies are routinely uploaded you may wish to maintain the input in its stream form (rather than buffering it like the first example above).

To maintain the stream resource something like this can be helpful:

<?php

function detectRequestBody() {
$rawInput = fopen('php://input', 'r');
$tempStream = fopen('php://temp', 'r+');
stream_copy_to_stream($rawInput, $tempStream);
rewind($tempStream);

return $tempStream;
}

php://temp allows you to manage memory consumption because it will transparently switch to filesystem storage after a certain amount of data is stored (2M by default). This size can be manipulated in the php.ini file or by appending /maxmemory:NN, where NN is the maximum amount of data to keep in memory before using a temporary file, in bytes.

Of course, unless you have a really good reason for seeking on the input stream, you shouldn't need this functionality in a web application. Reading the HTTP request entity body once is usually enough -- don't keep clients waiting all day while your app figures out what to do.

Note that php://input is not available for requests specifying a Content-Type: multipart/form-data header (enctype="multipart/form-data" in HTML forms). This results from PHP already having parsed the form data into the $_POST superglobal.

Accessing Variables in Raw PHP POST Data

Your post data is JSON so use json_decode to turn it into an array containing anobject and access the object_id property

$rawPostData = file_get_contents('php://input');
$json = json_decode($rawPostData);
$json = $json[0];
$all = date("F j, Y, g:i a") . " " . $json->object_id. "\r\n";
file_put_contents("Activity.log", $all, FILE_APPEND);

Get raw data sent by postman on php

$_POST array is for form data requests.

You need to read json from input:

$jsonData = json_decode(file_get_contents("php://input"), true);

How to create a logfile in php

To write to a log file and make a new one each day, you could use date("j.n.Y") as part of the filename.

//Something to write to txt log
$log = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
"Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
"User: ".$username.PHP_EOL.
"-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);

So you would place that within your hasAccess() method.

public function hasAccess($username,$password){
$form = array();
$form['username'] = $username;
$form['password'] = $password;

$securityDAO = $this->getDAO('SecurityDAO');
$result = $securityDAO->hasAccess($form);

//Write action to txt log
$log = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
"Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
"User: ".$username.PHP_EOL.
"-------------------------".PHP_EOL;
//-
file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);

if($result[0]['success']=='1'){
$this->Session->add('user_id', $result[0]['id']);
//$this->Session->add('username', $result[0]['username']);
//$this->Session->add('roleid', $result[0]['roleid']);
return $this->status(0,true,'auth.success',$result);
}else{
return $this->status(0,false,'auth.failed',$result);
}
}

Capture RAW (non decoded) $_GET data in PHP

You could use the $_SERVER['QUERY_STRING'] value and parse that.



Related Topics



Leave a reply



Submit