By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

php redirect: Warning: Cannot modify header information

P: n/a
Does the redirect statement:
header(Location:"http://www.newpage.com");
need to come before certain statements?

I've setup a login page and try to redirect a user once they have
logged in (after I set the appropriate $_SESSION value) but I get the
following error:

Warning: Cannot modify header information - headers already sent by
(...)

If I comment out the header everything works, I just don't get back to
the page I was on when I log in.

Thanks, Greg

Nov 11 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
JDS
On Fri, 11 Nov 2005 12:12:44 -0800, Greg Scharlemann wrote:
Warning: Cannot modify header information - headers already sent by
(...)


Have you *tried* Google?

This is a FAQ.

There is something -- whitespace, probably -- being printed to the browser
before the header() line(s).

header() must be the VERY FIRST thing sent to the browser. (which is not
the same as the first thing in the PHP script).

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Nov 11 '05 #2

P: 7
You need to put your script like this:
The php<tag> at the first line....

1. <?php>
2. header("Location: http://Your url ");
3.?>

Good Luck

http://www.halleswebdesign.com
Nov 15 '05 #3

P: 8
You can set session variables and then re-direct a user using the header. You just have to be careful to make sure you DO NOT output any text or anything to the browser prior to sending the header() function. Otherwise it willl result in the error you keep getting "headers already sent".

For example: [color=SeaGreen]THIS IS OK[/color] [PHP]
<?php
session_start();
// Start session and check if field foo was submitted from a form.
// If it was set a session variable to the value the user supplied.

if(isset($_POST['foo'])) {
$_SESSION['bar'] = $_POST['foo'];
} else {
$_SESSION['bar'] = false;
}

// Check if session var is false.
// If it is not false redirect user to the index page.

if(!$_SESSION['bar']) {
header("Location: index.php");
}

?>
[/PHP]

For example: [color=Red]THIS IS NOT OK[/color] [PHP]
<?php
session_start();
// Start session and check if field foo was submitted from a form.
// If it was set a session variable to the value the user supplied.

if(isset($_POST['foo'])) {
$_SESSION['bar'] = $_POST['foo'];
} else {
$_SESSION['bar'] = false;
}

// Check if session var is false.
// ** If it is not false tell user they are being redirected, **
// Then redirect user to the index page.

if(!$_SESSION['bar']) {
/* THIS IS WILL CAUSE AN ERROR */
echo "You are being redirected to the home page.";
header("Location: index.php");
}
?>
[/PHP]

So that's how the cookie crumbles. If you want to dive further into other possibilies you can check out some other php functions using output buffering [color=Gray][[color=Navy]ob_start(), ob_flush(), ob_get_contents()[/color]][/color]. It's a cakewalk once you get the hang of it.

Best of luck

~ Chipgraphics

[color=SeaGreen][color=Black][/color][/color]
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.