SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    May 2009
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The Darn Header Problem...

    Why is this wrong? Why do I get the darn Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\Yank\add_stuff.html.php:16) in C:\xampp\htdocs\Yank\index.php on line 24?

    Here's add_stuff.html.php:

    Code HTML4Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="?" method="post">
    DJUR:
    <input name="djur" type="text" id="djur" />
    PARK:
    <input name="park" type="text" id="park" /><br />
    <input name="knapp" type="submit" id="knapp" value="Lägg till!" />
    </form>
    </body>
    </html>

    And here's index.php:

    Code PHP:
    <?php
    $link = mysqli_connect('localhost', 'root', 'xxxxx');
    if (!mysqli_select_db($link, 'djur'))
    {
    $error = 'Unable to locate the animal database.';
    include 'error.html.php';
    exit();
    }
    if (!isset($_POST['djur']))
    {
    include 'add_stuff.html.php';
     
    $djur = mysqli_real_escape_string($link, $_POST['djur']);
    $park = mysqli_real_escape_string($link, $_POST['park']);
    $sql = 'INSERT INTO olika_djur SET
    djur="' . $_POST['djur'] . '",
    park="' . $_POST['park'] . '"';
    if (!mysqli_query($link, $sql))
    {
    $error = 'Error adding submitted joke: ' . mysqli_error($link);
    include 'error.html.php';
    exit();
    }
    header('Location: .');
    exit();
    }
    ?>

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason you are getting this error is because your order of logic is off.

    In order to fix this issue, you must do all your PHP logic before any HTML is out putted to the browser.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,048
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    You get error because you have sent output to the browser before making that call. You can't set headers after output has been sent to the browser.
    The only code I hate more than my own is everyone else's.

  4. #4
    SitePoint Zealot
    Join Date
    May 2009
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand THAT, but what is it inte code that "is off"?

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In order to fix this issue, you must do all your PHP logic before any HTML is out putted to the browser.
    Meaning do any include / print / POST statements before any HTML.

  6. #6
    SitePoint Zealot
    Join Date
    May 2009
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Cerium, but I think Oddz already said that. My question was if somebody could kindly point out where in the code I do this forbidden output.

  7. #7
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You problem may be coming from this part

    PHP Code:
    header('Location: .'); 
    You are out putting HTML (sending the HTTP Header) and THEN trying to set additional header information by trying to redirect the user.

  8. #8
    SitePoint Zealot
    Join Date
    May 2009
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I rearrange it like this, it works:

    Code PHP:
    if (!isset($_POST['djur']))
    {
    include 'add_stuff.html.php';
    exit();
    }
     
    include 'connect.php';
     
    $djur = mysqli_real_escape_string($link, $_POST['djur']);
    $park = mysqli_real_escape_string($link, $_POST['park']);
    $sql = 'INSERT INTO olika_djur SET
    djur="' . $_POST['djur'] . '",
    park="' . $_POST['park'] . '"';
    if (!mysqli_query($link, $sql))
    {
    $error = 'Error adding submitted joke: ' . mysqli_error($link);
    include 'error.html.php';
    exit();
    }
    header('Location: .');
    exit();

    I only wish I could understand why...

  9. #9
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because you are now checking for POST before any HTML is sent to the browser, how it should be.

  10. #10
    SitePoint Zealot Jaanboy's Avatar
    Join Date
    Sep 2007
    Location
    UK
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you already send ANY output to the browser, you will get that error message.

    In this case, the problem was caused by including the 'add_stuff.html.php' file (which outputs all that html code immediately) and then trying to redirect.

  11. #11
    SitePoint Zealot
    Join Date
    May 2009
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm... but does not my use of the exclamation change that?

    Code PHP:
    if (!isset($_POST['djur']))


    I mean, if you look at the last post with the code that works, I am still including the add.stuff file after the if statement. There must be something else that's causing the error message?!

  12. #12
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have any doubts about what output might have been sent, then try this simple debugging test:

    Comment out your header(), and echo something like "Redirecting to xxx.php"
    And then see what output you get when executing the script.. remember to look at the source code in the browser, to see if any HTML have been outputtet before the "Redirecting to xxx.php".

    If there are anything before the "Redirecting to xxx.php", you must change your script.

    Example of how you could test it:
    Code PHP:
    if (!isset($_POST['djur']))
    {
       include 'add_stuff.html.php';
       exit();
    }
     
    include 'connect.php';
     
    $djur = mysqli_real_escape_string($link, $_POST['djur']);
    $park = mysqli_real_escape_string($link, $_POST['park']);
    $sql = 'INSERT INTO olika_djur SET
       djur="' . $_POST['djur'] . '",
       park="' . $_POST['park'] . '"';
    if (!mysqli_query($link, $sql))
    {
       $error = 'Error adding submitted joke: ' . mysqli_error($link);
       include 'error.html.php';
       exit();
    }
    //header('Location: .');
    echo "Redirecting to .";
    exit();

    Any other output than "Redirecting to ." means you have to change your script.

    Try the above change, comeback here and post the resulting source code from the browser.
    zalucius


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •