php redirect: Warning: Cannot modify header information

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg Scharlemann

    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

  • JDS

    #2
    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/

    Comment

    • halles
      New Member
      • Nov 2005
      • 7

      #3
      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("Locatio n: http://Your url ");
      3.?>

      Good Luck

      Comment

      • chipgraphics
        New Member
        • Oct 2005
        • 8

        #4
        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("Locatio n: 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("Locatio n: 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]

        Comment

        Working...