Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP/Apache - how to see script warnings before header redirects?

Tags:

php

apache

I use a lot of header redirects but because of that I miss some PHP warnings that don't get shown due to redirect. Is there some PHP/Apache configuration option that would stop header redirects if any warnings are issued?

In fact I would like any warnings to stop script execution (like errors do) on my development machine but to be invisible on other servers - so that means some configuration setting and not changing of scripts. Is that possible? I'm using Apache 2.2 and PHP 5.2

All warnings are displayed normally on my development server, but when they're followed by a header redirect, I can't see them. Example:

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
unset($undef);
echo $undef; // generates warning, should stop redirect.
//exit; 
//when previous line is uncommented, I see the warning
header("Location: http://www.google.com"); // still happens, but shouldn't
exit;
?>
like image 463
A-OK Avatar asked Jun 19 '26 03:06

A-OK


1 Answers

On your development servers put:

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
ob_start();
...

if(ob_get_length() > 0){
    ob_flush();
    exit();
}
header(...);

before your header redirects (as shown). If anything is in the buffer it will be displayed and this will cause your header redirect to fail because it is impossible to do a header redirect if something has been rendered to the browser. If you turn display_errors to Off on your production servers, then these messages will not display.

like image 58
afuzzyllama Avatar answered Jun 21 '26 17:06

afuzzyllama