tags:

views:

52

answers:

2

I want to dump request variables to a file for debugging. How's this possible?

+2  A: 
<?php
$req_dump = print_r($_REQUEST, TRUE);
$fp = fopen('request.log', 'a');
fwrite($fp, $req_dump);
fclose($fp);

Untested but should do the job, just change request.log to the file you want to write to.

46Bit
Tiny insignificant detail, but Forgot your closing ?>
Josh
Closing ?> is not necessary, at all. Indeed it's best practice when writing libraries/etc (not that this is relevant here) to omit it so as to ensure no accidental output of whitespace that could mess up output buffering/headers/etc.
46Bit
I like `var_export($var,true)` personally.
Cole
@josh A closing ?> isn't necessary, actually its not good practice. You can get extra whitespace after the closing tag which can cause errors in your script
Stoosh
Interesting note. I learned something new.
Josh
+2  A: 

Use serialize() function for dumping. Dump $_SERVER, $_COOKIE, $_POST and $_GET separately (may go to the same file). If you're planning on debugging with the data it helps to know if the data was part of a POST request or a GET request.

Dumping everything is good for debugging in development, but not so in production. If your application does not have many users, it can work in production too. If you anticipate many users, consider dumping just the $_POST data, or limit server variables to those starting with HTTP_.

jmz