The online racing simulator
PHP header already sent
(4 posts, started )
PHP header already sent
Not really LFS related but I'm sure someone here can point me in the right direction! Have done a few google searches but can't find anything similar to my particular problem.

Error Message

<?php 
Warning
Cannot modify header information headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/DOPNEW/admin/index.php:45in /Applications/XAMPP/xamppfiles/htdocs/DOPNEW/admin/index.php on line 3
?>


What is throwing me with the error message is the fact that the offending code is line 45 but the header statement is line 3. Line 45 is where the next <?php opening tag is, the section with the 'switch' statements. I'd understand the problem if I had something before I called the header statement . The function test is being called from within the included 'add.php.'

Also forgot to mention that the function is called on a 'post' submit.


<?php 
php
function test(){
    
header("Location: index.php");
}

<!
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>
<
div id="header">
    <
div id="headerLogo"><img src="../web/dop-logo-admin.png" alt="dop logo" /></div>
    <
div id="headerSearch">
        <
form action="#" method="post">
            <
div>
                <
input type="text" name="headSearch" value="test" />
                <
select name="searchPlace">
                    <
option>Test</option>
                    <
option>Test 2</option>
                </
select>
            </
div>
        </
form>
    </
div>
    <
div id="headerInfo"></div>
</
div>
<
div id="container">
<
div id="leftNav">
    <
ul>
        <
li><a href="#" title="products admin">Products</a></li>
        <
li><a href="#" title="products admin">Stock</a></li>
        <
li><a href="#" title="suppliers admin">Suppliers</a></li>
        <
li><a href="#" title="categories admin">Categories</a></li>
        <
li><a href="#" title="customers admin">Customers</a></li>
        <
li><a href="#" title="settings">Settings</a></li>
    </
ul>
</
div>

<
div id="rightContent">
php
            $section 
$_GET['section'];
            switch(
$section){
                case 
'product':
                    
$action $_GET['action'];
                    switch(
$action){
                        case 
'add':
                            include_once(
'product/add.php');
                        break;
                    }
                break;
            }
        
</
div>
<
div id="footer">Test</div>
</
div>
</
body>
</
html
?>

Problem answered, I think!

The issue would be that the function is being called from within 'add.php.' By the time php get round to parsing it the header is already sent. Bit of a dumb moment I think.
Keiran: That error occurs when you are trying to modify headers after something has started output to the page. (echo, print_r, etcera).

It also happens in really weird things too. For instance:

<?php 
php
//CODE HERE

<NEW LINEWUT>
?>

That new line can also cause header issues, which is why it's encouraged, especially if a whole file is PHP.. to omit the closing PHP tag to prevent weird header issues.

Realistically, you should do headers first, and then do your output.. but this isn't always possible. A workaround is to use the Output Buffering methods. These create output contexts that only output to the buffer when you call any of the appropriate methods (ob_flush).


These can also allow you to do some useful things for instance: fputcsv only operates on filehandles.. however you can do this:


<?php 
$array 
= array(array('col1','col2'),array('what','up'));
ob_start();
$fh fopen('php://output','w+');
foreach(
$array as $row){
fputcsv($fh,$row);
}
$csv ob_get_clean();
?>

What that does, is because fputcsv only operates on file handles (stupid there's no generic version too).. is you can use output buffering to write to the output buffer, and then get the output as a string

The other thing is, if you're using a codebase that's a bit fubar.. is you can do an ob_start() at the beginning of your application, then ob_flush() at the end.. so that you don't run into header issues. However this isn't a good idea, and if your code is that messy.. you should fix the code, not hack around it.

I went a bit overboard, especially after you said you fixed it.. but I hope this explains more about the issue you were running into, and some ways around it (if you have code that is old/unmaintainable and you really don't want to refactor it properly).
Not at all Dawesdust, thanks for the information .

Was trying to be clever and have one file which contains my page styling for admin page and then use select to call in the required admin page. Going to restructure to solve this problem!

PHP header already sent
(4 posts, started )
FGED GREDG RDFGDR GSFDG