Connecting Tech Pros Worldwide Help | Site Map

php redirect: Warning: Cannot modify header information

  #1  
Old November 11th, 2005, 08:25 PM
Greg Scharlemann
Guest
 
Posts: 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

  #2  
Old November 11th, 2005, 08:35 PM
JDS
Guest
 
Posts: n/a

re: php redirect: Warning: Cannot modify header information


On Fri, 11 Nov 2005 12:12:44 -0800, Greg Scharlemann wrote:
[color=blue]
> Warning: Cannot modify header information - headers already sent by
> (...)[/color]

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 | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

  #3  
Old November 15th, 2005, 03:10 AM
Newbie
 
Join Date: Nov 2005
Posts: 7

re: php redirect: Warning: Cannot modify header information


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
  #4  
Old November 17th, 2005, 08:32 AM
Newbie
 
Join Date: Oct 2005
Posts: 9

re: php redirect: Warning: Cannot modify header information


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]
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
redirect print/Cannot modify header information - headers alreadysent JRough answers 0 September 26th, 2008 12:55 AM
Warning: Cannot modify header information webandwe answers 3 December 14th, 2007 11:48 AM
Cannot modify header information - headers already sent by maya answers 3 June 23rd, 2006 05:25 AM
Cannot modify header information - headers already sent by J Huntley Palmer answers 5 May 15th, 2006 10:05 PM