Warning: Cannot modify header information - HELP!!!

Discussion in 'PHP' started by netpox, Jan 11, 2007.

  1. #1
    My site was working fine and now all of the sudden i get this error message:

    Warning: Cannot modify header information - headers already sent by (output started at /home/mysite/public_html/dirx/dirxx/dirxxx/header.php:14) in /home/mysite/public_html/dirx/dirx/dirxxx/file.php on line 41

    I googled the error and got something about blank spaces but can't find anything, i made sure all the blank spaces were gone and still the error.

    Anyone know how to fix it?
     
    netpox, Jan 11, 2007 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The error is not just blank spaces... it's any output (so a PHP echo / print, an HTML tag, a blank space or even a new line).

    Essentially, as the error says, header.php on line 14 has outputted something. Now that may be because you printed something to the browser, or it could be because you put a new line after the closing tag.
     
    TwistMyArm, Jan 11, 2007 IP
  3. netpox

    netpox Active Member

    Messages:
    1,625
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    90
    #3
    I have a backup of the site on a different server and it works there so i ftp the working ones to the server where it stopped working and it still doesnt work? why would it work on one server and not on the other. Permissions are set the same.
     
    netpox, Jan 11, 2007 IP
  4. pachecus

    pachecus Well-Known Member

    Messages:
    1,841
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    110
    #4
    post the code so someone could tell you how to fix it
     
    pachecus, Jan 11, 2007 IP
  5. netpox

    netpox Active Member

    Messages:
    1,625
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    90
    #5
    Ok i replacted the .php file with the original that works on my other server. Here is the file. without doing any mods it throws an error message saying line 45

     
    netpox, Jan 11, 2007 IP
  6. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You have all this output:
    <?php
    include "header.php";
    ?>
    
    <table width="750">
    <tr>
    <td width="375" valign="top">
    <center>
    <img src=/images/image.gif"><br><Br>
    Code (markup):
    and then after that you're trying to send a cookie. Cookies are send in headers, hence the error message.

    I'd assume the difference between hosts is not down to the code working or not as it won't work in any case, it'll simply be one is setup to display errors and the other isn't.

    Move the output so that it comes after the cookie has been set.

    <?php
    // Vars! //
    require './include.php';
    $Score = 0;
    // End of Vars! //
    /**
    * ***********COOKIE STUFF!**************
    * What this will do is first check to see
    * if there is a cookie that has data in
    * it. Next if there is no cookie we then
    * calc the score and enter that data into
    * the cookie.
    */
    $i = unserialize($_COOKIE['Quest']); //Unserialize the data in the cookie (if there is one) and put the array in the var "$i"
    if ($i[TotalScore]) { // If there is a data from the cooke do this...
    $TotalScore = $i[TotalScore]; //Put data from the score in the cookie
    $CookieData = serialize(array ("QuizID" => $QuizID, "Taken" => "Yes", "TotalScore" => $TotalScore)); //Load up the data!
    setcookie("Quest", $CookieData, time() + 60 * 60 * 24 * 30); //Bake the cookie!
    } else {
    // This will clac the score..
    foreach($Quiz as $Question) {
    // This is a quite verison of the answer checker as show above...
    $QuestionArray++;
    $Var = "Q" . $QuestionArray;
    // This var is for the ammount of questions this will be used to calc the grade
    $QuestionAmt = $QuestionAmt + 1;
    if (stripslashes($$Var) == stripslashes($Quiz[$QuestionArray]['Answer'])) {
    $Score = $Score + 1;
    }
    }
    $TotalScore = round($Score / $QuestionAmt * 100); // Calc the score and round it
    $CookieData = serialize(array ("QuizID" => $QuizID, "Taken" => "Yes", "TotalScore" => $TotalScore)); //Cookie data
    setcookie("Quest", $CookieData, time() + 60 * 60 * 24 * 30); //Bake it!
    }
    // Clear dem vars!
    $Question = "";
    $Var = "";
    $QuestionArray = "";
    $QuestionAmt = "";
    /*
    **********END OF COOKIE STUFF!***********
    */
    include "header.php";
    ?>
    
    <table width="750">
    <tr>
    <td width="375" valign="top">
    <center>
    <img src=/images/image.gif"><br><Br>
    
    
    <?php
    /**
    * ******GET THE AVG SCORE FUNCTION******
    */
    function AvgScore($Score, $Type, $Rank){
    $ScoreFile = "./score.dat";
    $Number = $Score;
    if (is_writeable($ScoreFile)) { //Fist make sure the file is writable.
    if (!$fp=fopen($ScoreFile, "a+", filesize($ScoreFile))) { //Make sure you can open the file.
    exit("<b>Error: </b>Could not open $File");
    }else{
    $Number = $Number . "\n";
    fwrite($fp, $Number); //Wirte the score to the next line.
    fclose($fp); //close the file!
    }
    $Numbers = file($ScoreFile); //Load the whole file into an array called $Numbers
    $NumberSum = array_sum($Numbers);
    $NumberCount = count($Numbers);
    $NumberAvg = round($NumberSum/$NumberCount); //Devide the per-added numbers and round them...
    
    if ($NumberAvg=="" or $NumberAvg<=0) { //Make sure its okay to divide
    exit("Your are the first perosn to take this $Type<br>\n");
    }elseif ($Type == "Test") { //This is for the average score output for tests
    echo "Out of $NumberCount people that took this quiz the average score is <b>$NumberAvg%</b><br>\n";
    }elseif ($Type == "Quiz"){ //Thisis for the average score output for a quiz
    if ($NumberAvg <= 25 and $NumberAvg >= 0) {
    echo "<br>Out of $NumberCount people the average level was <b>" . $Rank[1] . "</b><br>\n";
    $RankTmp = 1; //This is tmp var that will be used when and if there is a html output.
    } elseif ($NumberAvg <= 50 and $NumberAvg >= 26) {
    echo "<br>Out of $NumberCount people the average level was <b>" . $Rank[2] . "</b><br>\n";
    $RankTmp = 2;
    } elseif ($NumberAvg <= 75 and $NumberAvg >= 51) {
    echo "<br>Out of $NumberCount people the average level was <b>" . $Rank[3] . "</b><br>\n";
    $RankTmp = 3;
    } elseif ($NumberAvg <= 100 and $NumberAvg >= 76) {
    echo "<br>Out of $NumberCount people the average level was <b>" . $Rank[4] . "</b><br>\n";
    $RankTmp = 4;
    }
    }
    }
    /**
    * ******END OF THE AVG SCORE FUNCTION******
    */
    }
    ?>
    
    <title><?php echo "$Title $Type - Page 2";
    
    ?></title>
    </head>
    <body>
    <?php
    $i = unserialize($_COOKIE['Quest']);
    if ($i[QuizID] == $QuizID and $i[Taken] == "Yes") {
    echo "<b>Error: </b>Sorry you can only take the $Type one time.<br>\n";
    if ($Type == "Test") {
    echo "Remember you got a <i>$TotalScore%</i> on your last test.<br>\n";
    }
    } else { // They have passed all the cookie shit now run like the wind!
    if ($Type == "Test") {
    foreach($Quiz as $Question) {
    // Besauce this is a test it's output will be very verbosy (har har)
    $QuestionArray++;
    /* $Var is to combine $Q* with $QuestionArray to yeild somthing
    * like $Q2 and then it is echo in the next line... This is done
    * becasue this var but be dynamicly changed
    */
    $Var = "Q" . $QuestionArray;
    // This var is for the ammount of questions this will be used to calc the grade
    $QuestionAmt = $QuestionAmt + 1;
    if (stripslashes($$Var) == stripslashes($Quiz[$QuestionArray]['Answer'])) {
    
    $Score = $Score + 1;
    } elseif ($$Var != $Quiz[$QuestionArray]['Answer']) {
    
    }
    
    }
    // $TotalScore = round($Score/$QuestionAmt*100); // this is not needed b/c the cookie thing does it!
    if ($Name) { // Check for a name...
    echo "You are <b>" . $TotalScore . "</b>% " . $Name . "\n<br><br>\n";
    } else { // if no name then output generic style
    echo "Score: <b>" . $TotalScore . "</b>%\n<br>\n";
    }
    if ($Store=="Yes") { //Check if they want to store the scores
    @AvgScore("$TotalScore", "Test"); //Use the @ becasue there should be 3 values but the third one is not needed for the test
    }
    // Now for the HTML text area in the test output.
    if ($HTMLOut == "Yes") {
    echo "<br>\n<font size=2>Copy and paste this into your own page, blog or bulletin:<br>\n";
    
    
    echo "<textarea name=\"HTMLOut\" cols=\"40\" rows=\"5\" onClick=\"this.focus();this.select()\">\n";
    echo "<center><a href=\"http://www.mywebsite.com\"><img src=\"http://www.mywebsite.com/image.gif\" border=0></a><br>\n";
    echo "<center><b><font size=3>$TotalScore%</b></font><br><a href=\"http://www.mywebsite.com\">Click Here</a> to take the test<br>\n";
    
    echo "</textarea>\n";
    } //End on the HTML out for test
    } elseif ($Type == "Quiz") {
    if ($TotalScore <= 25 and $TotalScore >= 0) {
    echo "<b>" . $Rank[1] . "</b>";
    $RankTmp = 1; //This is tmp var that will be used when and if there is a html output.
    } elseif ($TotalScore <= 50 and $TotalScore >= 26) {
    echo "<b>" . $Rank[2] . "</b>";
    $RankTmp = 2;
    } elseif ($TotalScore <= 75 and $TotalScore >= 51) {
    echo "<b>" . $Rank[3] . "</b>";
    $RankTmp = 3;
    } elseif ($TotalScore <= 100 and $TotalScore >= 76) {
    echo "<b>" . $Rank[4] . "</b>";
    $RankTmp = 4;
    }
    if ($Store=="Yes") { //Check if they want to store the scores
    AvgScore("$TotalScore", "Quiz", $Rank);
    }
    // Now for the HTML text area in the quiz output.
    if ($HTMLOut == "Yes") {
    echo "\n<br>\nCopy and paste this text into your own web site or blog.<br>\n";
    echo "<textarea name=\"HTMLOut\" cols=\"40\" rows=\"5\">\n";
    echo "<center>I am a $Rank[$RankTmp]<br>\n";
    echo "Click to take the <a href=\"http://" . $HTTP_HOST . dirname($_SERVER["REQUEST_URI"]) . "/\">$Title quiz!</a></center>\n";
    echo "</textarea>\n";
    } //End on the HTML out for quiz
    } else {
    echo "<b>Error: </b>Type mismatch\n<br>\n";
    }
    }
    
    ?>
    </body>
    </html>
    <center><br>
    <a href="/index.php">Take a different test</a></center>
    
    <br>
    <font size="2">
    Share your quiz results with your friends on your favorite websites like MySpace, Xanga, Friendster, blogs or your personal web site!<br>
    <br>
    </td>
    
    <td width="375" valign="top">
    <?php
    include "rightresult.php";
    ?>
    
    </td>
    
    </td>
    </tr>
    </table>
    
    <table>
    <tr>
    <td><center>
    <?php
    include "bottom.php";
    ?>
    </center>
    </td>
    </tr>
    </table>
    
    </td>
    </tr>
    </table>
    
    </td>
    </tr>
    </table>
    
    <?php
    include "footer.php";
    ?>
    </body>
    </html>
    PHP:
     
    rodney88, Jan 11, 2007 IP
  7. netpox

    netpox Active Member

    Messages:
    1,625
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    90
    #7
    Thanks that got rid of the error but now the calculation always shows as zero.
     
    netpox, Jan 11, 2007 IP
  8. netpox

    netpox Active Member

    Messages:
    1,625
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    90
    #8
    Is it possible that my host might of updated something on the server? different version of php? anything? how in the world does a code that has been running for months work fine and now all of the sudden not work.
     
    netpox, Jan 11, 2007 IP
  9. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #9
    stab in the dark, on the very first line in add

    
    ob_start();
    
    PHP:
    ???

    EDIT: that is on the original version that used to work ...

    EDIT2 : Actually it's not a stab in the dark, the reason is some hosts have output buffering on by default, which would explain why you got away with doing the cookie after html becuase nothin is sent to the browser untill the script has completing executing, and by this time the cookie is set and everything else is already done .... fingers crossed ....
     
    krakjoe, Jan 11, 2007 IP