PDA

View Full Version : session_start() and header() in same document.


Steve887
07-16-2006, 09:11 AM
How can you get around the "headers already sent...." when you use both session_start() and header in your code?

Eg.

Line 1 <?php session_start();
2...59 (for example) various code
60 header ('Location: anotherpage.php'); ?>

I have been looking at ob_start() in the php manual but I'm not sure how to use it in this case. Can anyone help?

ns1987
07-16-2006, 10:06 AM
Put ob_start() before session start.

And I think the problem wasn't the fact that you send a header, but that you print something out before you sent it. Anyways, ob_start() would fix it.

GJay
07-16-2006, 11:04 AM
session_start();
if($_SESSION['loggedin']!=='yes')
header("Location: /login.php");


Is fine, you don't need to use output buffering.

lavinpj1
07-16-2006, 01:06 PM
Line 1 <?php session_start();
2...59 (for example) various code
60 header ('Location: anotherpage.php'); ?>

If lines 2-59 contain echo's/prints/anything else that sends data to the browser, you will need to use output buffers.

~Phil~