0

Hello I'm trying to get all the values from $_POST request and to log it.

This my code:

<?php

$file = 'error_log.log';

$info = date("Y-m-d  H:i:s")."  -  ".print_r($_POST, true)."\n";


foreach ($_POST as $key => $value) {
    foreach($value as $k => $v) {
        $info .=  $k."\n";
        $info .=  $v."\n";
     }
}

$current = file_get_contents($file);

$current .= $info;

file_put_contents($file, $current);

?>

But my problem is that all I get is "Array".

Like this:

2014-01-01  17:32:50  -  Array
(
)

2014-01-01  17:34:13  -  Array
(
)

2014-01-01  17:47:39  -  Array
(
)

2014-01-01  17:47:40  -  Array
(
)
3
  • What's a method in your form? Commented Jan 1, 2014 at 15:54
  • It's an empty array for $_POST, so something is not passing in from your form. That's all. Commented Jan 1, 2014 at 15:55
  • file_put_contents($file, $info, FILE_APPEND); Also you could just append to file so don't have to read it first, should be a little faster Commented Jan 1, 2014 at 16:06

2 Answers 2

2

The array comes from print_r($_POST, true);. $_POST is always an array and if array is empty that is the correct output you except. So basicly this means you don't send anything to as POST maybe you are using get ?

What you could do is check if anything is posted at all.

if (count($_POST) > 0) {
    $info .= print_r($_POST, true);
}

If you are redirecting user to this page in case of error, the POST values won't be redirected with the user. And if this is an error catching page, maybe you should get save $_SERVER['REQUEST_URI'] along the data, so you know in which page the error happend.

Also as I said in my comment you could optimize your file writing with this.

file_put_contents($file, $info, FILE_APPEND | LOCK_EX);

It locks the file, so other scripts can't write to it at a same time, and appends the info to the end of the file. So you could replace these lines with just one command.

$current = file_get_contents($file);
$current .= $info;
file_put_contents($file, $current);

The whole script

<?php
$file = 'error_log.log';
$info = date("Y-m-d  H:i:s") . " - " . $_SERVER['REQUEST_URI'] . "\n";
$info .= "POST: " . ((count($_POST))?print_r($_POST,true):"EMPTY\n");
$info .= "GET : " . ((count($_GET))?print_r($_GET,true):"EMPTY\n") . "\n";
file_put_contents($file, $info, FILE_APPEND | LOCK_EX);

Output example

2014-01-01  17:33:25 - /stack/error.php
POST: EMPTY
GET : EMPTY


2014-01-01  17:34:11 - /stack/error.php?ads=bsd&error=true
POST: EMPTY
GET : Array
(
    [ads] => bsd
    [error] => true
)
Sign up to request clarification or add additional context in comments.

Comments

1

You've made it more complex than it is. Instead of:

foreach ($_POST as $key => $value) {
 foreach($value as $k => $v)
{
 $info .=  $k."\n";
 $info .=  $v."\n";

 }
}

It's just

foreach ($_POST as $key => $value) {

 $info .=  $key."\n";
 $info .=  $value."\n";

}

2 Comments

Yes, and? That's the point of my answer, the OP is attempting to find values in a sub-array that doesn't exist.
@AlirezaFallah ok, no problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.