-2

What I need is a local web server appending all the POSTed data to a text file. The actual idea is to implement this (Storing into file using JavaScript/GreaseMonkey). I think the fastest way is to use XAMPP. The problem is I have forgotten PHP many years ago and have no idea how to do this, though I believe it is easy (no error handling or security required, just a quick-and-dirty one-time solution).

Would you be so kind to suggest a code for this?

Community
  • 1
  • 1
Ivan
  • 63,011
  • 101
  • 250
  • 382

1 Answers1

1

I may be thrown off by the mention of XAMPP in the question, but XAMPP is a pain. If you are able, installing a LAMP / MAMP / WAMP server setup manually is the way to go. Just Google the appropriate type (Windows, Linux, Mac) and you will find there's lots of good write-ups on how to achieve your installation.

Then, capturing the code with some PHP is trivial, and storing it to a file is trivial. It's fairly straightforward to loop over the post data:

$values = '';
foreach($_POST as $key=>$value) {
    $values.= $key . "=" . $value . "\t";
}

Then, writing it to the file:

$filename = 'my_post_data.txt';
$handle = fopen($filename, 'w');
fwrite($handle, $values);
fclose($handle);

This of course is very generic, but is intended as a primer to get you going on your project. Good luck.

random_user_name
  • 25,694
  • 7
  • 76
  • 115
  • Thank you very much, @cale_b, this is it. The only thing to add is that this replaces the file contents instead of appending - I had to replace the last 3 lines with `file_put_contents($filename, $values, FILE_APPEND);` to do the job (found here: http://stackoverflow.com/questions/12335885/php-add-string-to-text-file). – Ivan Dec 27 '13 at 09:01
  • And I still believe XAMPP is the fastest and easiest way. I have tried many ways to set a development AMP server up in my life and this is still my favourite, less than a minute to up and running the way I want. – Ivan Dec 27 '13 at 09:04