note 76375 modified in function.header by danbrown

From: Date: Sun, 23 Jan 2011 17:35:05 +0000
Subject: note 76375 modified in function.header by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176214@lists.php.net to get a copy of this message
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://mysite.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. --was-- 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://mysite.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://mysite.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. http://php.net/manual/en/function.header.php

« previous php.notes (#176214) next »