Header Location problem

Discussion in 'PHP' started by coledavis, May 22, 2011.

  1. #1
    Hi. I've previously written code like this without a problem, but now browsers seem to give me messages like this:
    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/jexlkzct/public_html/angloman.org/IndexProcessor.php:1) in /home/jexlkzct/public_html/angloman.org/IndexProcessor.php on line 1

    Warning: Cannot modify header information - headers already sent by (output started at /home/jexlkzct/public_html/angloman.org/IndexProcessor.php:1) in /home/jexlkzct/public_html/angloman.org/IndexProcessor.php on line 38

    Here is the file. Any advice would be greatly appreciated.

    <?php session_start();?>
    <?php ob_start();?>

    <?php

    // variable initialization

    $main = NULL;

    $_SESSION["interestVar"] = NULL;

    ?>

    <?php



    // getting form data if set and put into session

    if(isset($_POST["Question1"])) {

    $main = $_POST["Question1"]; $_SESSION["interestVar"] = $main;

    }


    // re-directing to page
    if ($main >= 3) {
    ob_end_clean();
    header("Location: number.php");
    exit();
    }
    elseif ($main == NULL) {
    ob_end_clean();
    header("Location: indexerr.html#error");
    exit();
    }
    elseif($main < 3 and $main > 0)
    {ob_end_clean();
    header("Location: price.php");
    exit();}

    ?>
     
    coledavis, May 22, 2011 IP
  2. phppro9x

    phppro9x Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Change

    header("Location: number.php");
    ob_end_clean();
     
    phppro9x, May 22, 2011 IP
  3. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Change
    
    <?php session_start();?>
    <?php ob_start();?>
    
    Code (markup):
    to
    
    <?php session_start();
      php ob_start();?>
    
    Code (markup):
    The carriage returns in the source code between the ?> and <?php tags are sent to the user before your ob_start() function get executed. That's why you can't use the header() function.
     
    rainborick, May 22, 2011 IP
  4. coledavis

    coledavis Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Thanks for the tries guys, but neither did it. It's funny, as previous web sites have worked well. Is this something to do with newer editions of PHP? (I previously programmed in PHP4 but my ISP now uses PHP5, although my old programs still work.)
    How about output buffering??
     
    coledavis, May 23, 2011 IP
  5. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Well, something is sending output to the user before your script encounters the header() function call. That's what the error message you see means. So eliminate any ?> <?php sequences and any echo()s that appear before the header() call.
     
    rainborick, May 23, 2011 IP
  6. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #6
    
    <?php
    ob_start();
    // You Complete Code
    ob_flush();
    ?>
    
    Code (markup):
     
    HungryMinds, May 23, 2011 IP