开发者

Include $_POST, $_GET, $_SERVER values in PHP Error Log

I have code like this

try {
    header("Location: http://www.google.com\n-abc");
}
catch (Exception $e) {
    error_log(print_r($_POST, true));
    error_log(print_r($_GET, true));
    error_log(print_r($_SERVER, true));
}

Without the try {} catch {} block, I can see the POST, GET and SERVER variables in my error_log, but with the try {} catch {} block, I only see the default PHP error.

  1. Is there a way to show the POST, GET, and SERVER variables in a try {} catch {} block?
  2. Is there a way to have PHP include POST, GET, and SERVER variables for ALL errors that get logged to file and not just wherever I have added 开发者_StackOverflowerror_log(print_r($_POST, true)); ....?


try/catch is for when you want to throw an exception to prevent php fatal errors from killing the page or to make debugging easier, generally speaking. This is how you would use a try/catch:

try {
    if($a === true)
    {
        header("Location: http://www.google.com\n-abc");
    }
    else
    {
        throw new Exception('$a was not true');
    }
}
catch (Exception $e) {
    error_log(print_r($_POST, true));
    error_log(print_r($_GET, true));
    error_log(print_r($_SERVER, true));
    echo $e->getMessage(); // $a was not true
}

The reason you don't see your error logs in the catch block in your example is because you never threw an exception so PHP will never look inside that catch block to log your variables.

If you simply want to get your example to work you'd just throw an exception to get PHP in that catch block:

try 
{
    throw new Exception('redirecting...');
}
catch (Exception $e) 
{
    error_log(print_r($_POST, true));
    error_log(print_r($_GET, true));
    error_log(print_r($_SERVER, true));
    header("Location: http://www.google.com\n-abc");

}

That's just silly though :)


But default PHP functions like header will not throw exceptions.

If you want them to throw exceptions you will need to set the error handler to use ErrorException:

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");


I think you have a few questions/issues here.

First, a try/catch block is used for Exceptions. The catch {} code is only executed if a Exception of the type specified is thrown.

Second, the header() function does not throw exceptions. Instead it just generates a PHP error directly.

Thirdly, to log those variables for all errors take a look at set_error_handler(), which should also take care of the above, second issue.


try/catch would only work if you're using an object that actually throws exception. header() is NOT an object and will NEVER throw an exception. Like I said in my answer to your previous version of this question, you need to set up a custom error handler.


That's how try/catch is supposed to work: the catch block executes when an exception is thrown in the try block (and only in that case).

The header() function may generate a warning if headers are already sent, but it will never throw an exception.

Answering your questions:

  1. Your catch block is just fine but you need code that can actually throw exceptions (typically object-oriented code, but not necessarily). However, you can configure PHP to convert regular errors into exceptions if you use set_error_handler() to create a custom error handler and use throw inside of it.

  2. Same as above: set_error_handler() and set_exception_handler().


Sounds like you want to have some extra information output to your log when an error is thrown. You should check out custom error handling in PHP. You can define specific ways to handle errors in your scripts and add functionality that is not there by default.PHP Set error handler

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜