chronister Posted January 17, 2008 Share Posted January 17, 2008 Why would I get a warning about header information when my script starts with ob_start();? Here is what I have the first 4 lines on every single page <?php ob_start(); session_start(); ini_set('error_reporting', E_WARNING); ?> The line that is causing the errror. <?php function checkProtected($permission_name) { if($_SESSION['Global Admin'] == 1 || $_SESSION[$permission_name] == 1) { //do nothing they can access this resource } else { header("Location: http://myhappyjoes.com/denied.php"); <-- error happens here } } ?> I though the ob_start() would take care of this type of error. Thanks, nate Quote Link to comment https://forums.phpfreaks.com/topic/86496-header-error-with-ob_start-on/ Share on other sites More sharing options...
kenrbnsn Posted January 17, 2008 Share Posted January 17, 2008 Do you have anything doing output before the ob_start() statement? What is the error? Ken Quote Link to comment https://forums.phpfreaks.com/topic/86496-header-error-with-ob_start-on/#findComment-441986 Share on other sites More sharing options...
chronister Posted January 17, 2008 Author Share Posted January 17, 2008 Here is the error : Warning: Cannot modify header information - headers already sent by (output started at /homepages/11/d207413455/htdocs/myhappyjoes.com/header.php:154) in /homepages/11/d207413455/htdocs/myhappyjoes.com/etc/includes/gen_functions.php on line 10 Lines 1-5 of header.php <?php ob_start(); session_start(); ini_set('error_reporting', E_WARNING); ?> line 152 - 154 of header.php } ob_end_flush(); ?> Here is how I call header.php <?php $page_title = 'Send Mail'; include($_SERVER['DOCUMENT_ROOT'].'/header.php'); checkProtected('Access E-Mail List'); ?> Line 10 of gen_functions.php is here <?php function checkProtected($permission_name) { if($_SESSION['Global Admin'] == 1 || $_SESSION[$permission_name] == 1) { //do nothing they can access this resource } else { header("Location: http://myhappyjoes.com/denied.php"); <-- line 10 } } ?> I get the error to go away by placing an ob_start(); at the top of the index.php page that this error is coming up on, but I think there is a better solution as I am essentially doubling up on the ob_start() functions. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/86496-header-error-with-ob_start-on/#findComment-442102 Share on other sites More sharing options...
kenrbnsn Posted January 17, 2008 Share Posted January 17, 2008 The ob_end_flush() at the end of the header.php sends output to the browser, that's why you're getting the error. Remove the ob functions from the header.php file and just use them in the main file. A better solution would be to move any header() calls to before you send output to the browser, since when you go to a new page that will wipe out any data that has been sent to the browser. It just doesn't make sense (in my mind) to send output to the screen and then go to a new page. Ken Quote Link to comment https://forums.phpfreaks.com/topic/86496-header-error-with-ob_start-on/#findComment-442121 Share on other sites More sharing options...
PFMaBiSmAd Posted January 17, 2008 Share Posted January 17, 2008 Sending content to the browser and then redirecting to a different page, just wastes your bandwidth every time you do it. Quote Link to comment https://forums.phpfreaks.com/topic/86496-header-error-with-ob_start-on/#findComment-442131 Share on other sites More sharing options...
janim Posted January 17, 2008 Share Posted January 17, 2008 ob_start() dosent solve the proplem all the time you need to use javascript in redirection the header("location : dddddd") must be in the first rows i think so java script is better Quote Link to comment https://forums.phpfreaks.com/topic/86496-header-error-with-ob_start-on/#findComment-442168 Share on other sites More sharing options...
revraz Posted January 17, 2008 Share Posted January 17, 2008 And what if they have Java disabled? It's better to fix the logic and use a header. ob_start() dosent solve the proplem all the time you need to use javascript in redirection the header("location : dddddd") must be in the first rows i think so java script is better Quote Link to comment https://forums.phpfreaks.com/topic/86496-header-error-with-ob_start-on/#findComment-442175 Share on other sites More sharing options...
chronister Posted January 17, 2008 Author Share Posted January 17, 2008 I have been battling this for a long while. The way I template my sites I have a header.php file that contains my <html><head><title></title></head><body></body></html>. I then have a function in the middle of the page which creates the footer out of the code that resides in header.php e.g. <html> <head> <title></title> </head> <body> <div id="banner">BANNER HERE</div> <div id="content"><!-- all my content will load here --><?php function footer(){ ?> </body> </html> <?php } ?> Then I have my pages like so... <?php include('header.php') ?> Content here <?php footer() ?> So by doing this, I can have 1 layout page and use that over and over again. But it also makes it so output has started on every page as soon as it starts including header.php. This is why I make ob_start() the first line of header.php and ob_end_flush() as the last line. Those 2 lines are the first and last to load respectively. Any suggestions on a better way that will still keep the 1 file template structure I have ? Thanks, Nate Quote Link to comment https://forums.phpfreaks.com/topic/86496-header-error-with-ob_start-on/#findComment-442187 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.