4

I'm trying to design a page which does some database actions, then redirects to user back to the page they came from. The problem is that I use a require() function to get the connection to the database, so the headers are already sent. A meta tag is out of the question since I want it to look like all the processes are done from the page they came from. Any tips? Is there a way I can use the require() and the header() or do I have to drop one? Is there an alternative to header()?

1
  • 8
    How does a connection to a database makes headers be sent? Commented Jan 22, 2011 at 17:04

6 Answers 6

7

If you can't send the header() before some content gets sent, use output buffering by placing an ob_start(); at the beginning of your script before anything is sent. That way, any content will be stored in a buffer and won't be sent until the end of the script or when you manually send the buffer's contents.

On another note, simply requireing another file would not generate any headers/content unless that included script sends them. The most common "hidden" cause of this is unnoticed whitespace before or after the <?php ?> tags.

Sign up to request clarification or add additional context in comments.

1 Comment

oh, so that's what a buffer does. I'd never know.
4

As Artefacto noted, connecting to the database should not require any output. Fix whatever you're including (e.g. database_connect.php) not to output. See this search on the "headers already sent" issue, which may help you find "hidden" output.

1 Comment

You were absolutely right. I found an extra space after my ending php tag in my required file.
2
ob_start(); // start output buffering
echo "<html......"; // You can even output some content, it will still work.
.
.
.
.
.
header("Location: mypage.php");
ob_flush(); //flush the buffer

In this case, all output is buffered. This means, the headers are processed first, then the output comes to play...

Comments

1

You cannot send any headers after some content has already been sent. Move the header() call to be before the require() call.

1 Comment

Not possible as the script has to do some database action THEN redirect.
0

You cannot send headers after any data has been sent to the client.

However, using require does not meen that you output something. If i understand your right, you can include your database files, run your queries and then redirect the user. This is perfectly valid.

If you need to send some output (why if you need to do a redirect?) another option is to use output buffering. By using output buffering, you're not sending the data to the browser when you echo it, but you store it in a buffer. The data will be sent when you call ob_end_flush or you reach the end of the script. After ob_end_flush, you won't be able to send any new headers. You start output buffering with ob_start.

1 Comment

I'm not sending output. The require() seems to send the headers automatically. I have no output text.
0

It is possible to use header() with require() when I use output buffering. That means that the whole script is buffered and first send when the script has come to an end.

I have done it by doing this

ob_start("ob_gzhandler");  //begin buffering the output

require_once('/classes/mysql.php');

// Some code where I access the database.

header('/somepage.php');
exit;

ob_flush(); //output the data in the buffer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.