Connecting Tech Pros Worldwide Help | Site Map

php redirect: Warning: Cannot modify header information

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 11th, 2005, 07:25 PM
Greg Scharlemann
Guest
 
Posts: n/a
Default php redirect: Warning: Cannot modify header information

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, 07:35 PM
JDS
Guest
 
Posts: n/a
Default 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, 02:10 AM
Newbie
 
Join Date: Nov 2005
Posts: 7
Default 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, 07:32 AM
Newbie
 
Join Date: Oct 2005
Age: 30
Posts: 9
Default Just a quick self check...

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]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.