2

Possible Duplicate:
Headers already sent by PHP

error:

Warning: Cannot modify header information - headers already sent by (output started at functions.php:37) in functions.php on line 207

add.php

<?php include('functions.php'); ?>
<?php global_header("Add"); ?>
    <?php page_navigation(); ?>
    <?php
    // If no form has been submitted, present form
    if (empty($_POST))
    {   
        add_form();
    }
    // if a form has been submitted
    else
    {       
        // if form_validity() == 1, proceed to connect
        if (form_validity() == 1)
        {
            // connect to mysql + database
            connect();

            // get values from form, insert into database

            $saleItemCheck = isset($_POST['saleItem'])?"y":"n";
            $discItemCheck = isset($_POST['discountedItem'])?"y":"n";

            $sql=("INSERT INTO inventory (name, manufac, model, descrip, onhand, reorder, cost, price, sale, discont, deleted)
                       VALUES ('$_POST[itemName]', '$_POST[manufacturer]', '$_POST[model]', '$_POST[description]', '$_POST[numberOnHand]', 
                               '$_POST[reorderLevel]', '$_POST[cost]','$_POST[sellingPrice]', 
                               '$saleItemCheck', '$discItemCheck', 'n')");

            // if the query doesn't work, display error message
            if (!(mysql_query($sql))) { die ("could not query: " . mysql_error()); }

            add_form(); 

            redirect("view.php");
        }
        else
        {
            // if form is not valid (form_validity returns 0), display error messages
            add_form();
        }
    }
    ?>
<?php page_footer(); ?>

My redirect function

<?php
function redirect($page)
{
header('Location:'.$page); <------------------------------------ line 207
}?>

Header function

<?php
function global_header($page_title)
{
    $content = '<!doctype html>
    <html lang="en">
        <head>
            <meta charset="utf-8">
            <title>DIMAS OLYMPIC WEIGHTLIFTING EQUIPMENT - ' . $page_title . '</title>
            <meta name="description" content="BTI320 Assignment 1">  
            <meta name="author" content="Marcel Olszewski - 078-681-103"> 
            <link rel="stylesheet" href="style.css" />
        </head> 
        <body>
        <div id="container">
        <div><img src="logo.png" id="logo" alt="I (Marcel Olszewski) created this in photoshop" /></div>';
    echo $content;  <------------------- LINE 37
}
?>

It worked before, doesn't now, not too sure why.

Community
  • 1
  • 1
eveo
  • 2,797
  • 15
  • 61
  • 95

3 Answers3

1

You don't need to close and re-open the PHP tag -- this:

       }
}
?>
<?php page_footer(); ?>

can be converted to:

   }
}
 page_footer(); 

You don't even need the closing PHP tag. By closing the tag you run the risk of leaving whitespace after the closing tag which will be echoed out to the browser and will cause the issue you're having. In general, you don't need to close your PHP tags if they come at the end of a file.

Edit: Here's your problem:

Change:

<?php include('functions.php'); ?>
<?php global_header("Add"); ?>
    <?php page_navigation(); ?>
    <?php

to:

<?php include('functions.php'); 
global_header("Add"); 
page_navigation(); 
StackOverflowed
  • 5,854
  • 9
  • 55
  • 119
  • Thanks for solving a problem I didn't have in the first place I guess? – eveo Oct 20 '12 at 02:54
  • What I'm saying is that if you close your PHP tag and there's a space (or line break, etc) after your closing tag, that would be considered output and then if you try to send headers it will display the error you're receiving. – StackOverflowed Oct 20 '12 at 02:58
  • 1
    I apologize for being rude. However, the error message only mentions my functions file and I haven't changed that footer since the projects inception, yet somehow redirect seemed to work before. – eveo Oct 20 '12 at 03:01
  • Just saw your problem, please see edit. – StackOverflowed Oct 20 '12 at 03:03
  • That did not work either, thanks though :( – eveo Oct 20 '12 at 03:12
  • I'm done with this. I already wasted too much time on it. Scrapping this project. Fed up with PHP. – eveo Oct 20 '12 at 03:14
  • But now you know what to look for, do text searches for " " (ie spaces before opening tags and after closing tags – StackOverflowed Oct 20 '12 at 03:15
  • Just tried that. Still doesn't work. I'm done with this language for good now. – eveo Oct 20 '12 at 03:17
0

This is because you have already outputed some stuff to the stdout. Look at your code. If you have not explicitly done that, it might be that there are errors in the execution of your code (like MySQL connection error, for instance) and the display_errors is On in your php.ini making the error messages being written prior to your header call.

Also, if the encoding of your PHP code has changed, a BOM character might have been prepended to your code, being an involuntary, but very real output before you send your headers.

Another thing that could have caused the problem, is having close-open ?><?php tags which might leave whitespaces (or even other characters) in your code in between the PHP blocks, sending unnecessary output to the browser across HTTP, causing the problem.

Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
  • It worked fine before, not sure where I went wrong. – eveo Oct 20 '12 at 02:43
  • As I said, it might be that you are getting errors now that were not present before. Also, if the encoding of your PHP code has changed, a BOM character might have been prepended to your code, being an involuntary, but very real output before you send your headers. – Milad Naseri Oct 20 '12 at 02:44
  • Used Notepad++ to get rid of BOM characters, still doesn't work. – eveo Oct 20 '12 at 02:49
0

It looks you had output buffering on before because you definitely have output before you try to redirect.

Musa
  • 96,336
  • 17
  • 118
  • 137