Jump to content

header error with ob_start() on


chronister

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.