note 76375 deleted from function.header by tularis

From: Date: Fri, 04 Apr 2014 11:30:06 +0000
Subject: note 76375 deleted from function.header by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199167@lists.php.net to get a copy of this message
Note Submitter: jasper at jtey dot com ---- If you are finding that header() is not working for no obvious reason, make use of the headers_sent() function to drill down to the cause of the problem. Consider the following scenario, <?php // Sign out signOut(); // Redirect back to the main page header("Location: http://example.com/mainpage"); ?> If for some reason, the header() call to redirect is not working, there won't be any error messages, making it difficult to debug. However, using headers_sent(), you may easily find the source of the problem. <?php // Sign out signOut(); /* * If headers were already sent for some reason, * the upcoming call to header() will not work... */ if(headers_sent($file, $line)){ // ... where were the mysterious headers sent from? echo "Headers were already sent in $file on line $line..."; } // Redirect back to the main page header("Location: http://example.com/mainpage"); ?> As the documentation states, "header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP." In the above debugging solution, you will find out exactly where any of these problematic output or blank lines exist. You should then be able to resolve the issues with much more ease than if you hunted for the problems aimlessly.

« previous php.notes (#199167) next »