-4

I am getting this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\header.php:166) in C:\wamp\www\stuffs.php on line 86

Header.php

             <aside id="route_header">
                <ul>
                <li id="welcome">
                        <a href="<?php echo $username_db?>"><?php echo "Hello $fname_db";?></a>
                </li>
                <li id="forback">
                    <span id="click_requests">
                        <img src="images/group_req.png" width="25px" height="25px">
                        <span id="request_number">0</span>
                    </span>
                    <div id="group_request_wrapper">
                        <div>
                            <span>
                                <div>

                                </div>
                                <div>
                                </div>
                                <div>
                                </div>
                                <div>
                                </div>
                            </span>
                        </div>
                        <div id="read_all_requests">
                            <a href="#">Read all</a>
                        </div>
                    </div>
                </li>
                <li id="search_form_header">
                    <form role="search" id="search_form" method="get">
                        <input type="search" placeholder="Search..."/> 
                    </form>
                </li>
                <li id="personalFLink"><a href="/personal">Personal</a></li>
                <li id="settings">
                    <a href="/settings">Settings</a>
                    <!--<img class="settings_image" src="images/settings.png" width="20px" height="20px">
                    <ul id="drop_down_settings">
                        <li>Settings</li>
                        <li>Privacy</li>
                    </ul>-->
                </li>
                <li id="logout_header"><a href="/end_session.php">Logout</a></li>
                </ul>
            </aside>
            </h1>
        </header>
        <div id="main_bottom_wrapper">

where

<div id="main_bottom_wrapper>

is my line 166 in header.php.

This is half the code. But the thing is, my header was working perfectly until I added the following code in header.php :

                            <span>
                                <div>

                                </div>
                                <div>
                                </div>
                                <div>
                                </div>
                                <div>
                                </div>
                            </span>

I don't know what but it has everything to do with these lines code. I tried to replace the div with ul/ li/ span/ but nothing is working.

I might be missing out the obvious but I just can't seem to find the solution.

stuffs.php

if(mysqli_query($con, $gcontent_query)){
    header("Location:/".$group_url."&".$fname_key);
    exit();
}

This is my line 86.

P.S: There are no whitespace/newline issues so skip that answer. No echo/print error because it did work before, but after adding that code it won't work anymore.

halfer
  • 19,824
  • 17
  • 99
  • 186
bayblade567
  • 270
  • 1
  • 10
  • So is stuffs.php loaded after header.php? Because it looks to me like header.php is outputting some html/text. And if that is the case, you can't call the header function in stuffs.php because that is considered output before the call to header. – Jonathan Kuhn Dec 10 '14 at 18:14
  • `headers already sent` generally always means you've entered some HTML **before** writing your PHP headers. Remove those offending lines and see if that fixes it. My guess is the error does **not** have anything to do with those lines you added. – Phil Tune Dec 10 '14 at 18:14
  • If you've already read other questions, then you would realize that "headers already sent" is only a symptom, and that choosing it as the title for this question is only going to result in other users thinking that you don't really understand what your issue is. You may want to change your title, since "headers already sent" is the php equivalent of "this code is broken". – Lynn Crumbling Dec 10 '14 at 18:15
  • All the advice about whitespace is really about HTML, it's just that whitespace gets mentioned so often because it's easy to overlook. – TecBrat Dec 10 '14 at 18:16
  • stuffs.php includes **header.php** before the header() is executed. And like I said it was working before I added those lines of code....how is it related to those lines? @JonathanKuhn – bayblade567 Dec 10 '14 at 18:16
  • @LynnCrumbling That is incorrect. It is a very helpful error message. It says "Hey, you sent some HTML and then tried to send headers!". Much more helpful than "It's broken." – TecBrat Dec 10 '14 at 18:17
  • @LynnCrumbling then what should the title say? I can't really see the issue. – bayblade567 Dec 10 '14 at 18:18
  • it doesn't. Previously there was likely something like output buffering that captures all output before being sent out and stores it in a buffer and sends it at the end of the script. This allows you to make header calls because nothing was actually sent to the user (it was buffered). This can be a php.ini setting or a call to ob_start(). – Jonathan Kuhn Dec 10 '14 at 18:18
  • 2
    Seems like you answered it... "**header.php** [sent] *before* the *header()* is executed." – Phil Tune Dec 10 '14 at 18:19
  • ^^^ this -- what @philtune said. – Lynn Crumbling Dec 10 '14 at 18:19
  • Could anyone exactly elaborate because am not a php expert, and how is **header.php** outputting anything? Every time I remove those lines, it works again, so what exactly is happening here? @philtune but I did include header.php the same way before adding those lines, and it did seem to work. Please explain. – bayblade567 Dec 10 '14 at 18:22
  • 1
    Your post shows header.php is 100% HTML... Writing = outputting. Send your request headers before you write/include/echo **anything at all**!! – Phil Tune Dec 10 '14 at 18:25
  • @LynnCrumbling...yes exactly my code works. Just the header() ain't working. And I want to redirect using php. Plus, i got numerous other tags in **header.php**, why only those tags affecting header() ? – bayblade567 Dec 10 '14 at 18:26
  • @bayblade567 I rephrased my comment, and added a bit more. Not sure if it'll help, but ... – Lynn Crumbling Dec 10 '14 at 18:27

2 Answers2

2

The error is effectively that the second you add any output at all (including whitespace, or in this case, tags), you can no longer call header after that. As a test - comment out your header line temporarily. You'll see that your code works.

header("Location:/".$group_url."&".$fname_key);

Will ALWAYS crash your code if you've sent anything else. Comment it out to prove that to yourself. Your code will magically start working again until you uncomment it.

Most likely, you need to re-engineer your code to either send html (<div>, <span>, and the like) OR call the header() function, but not both. The other alternative is to move the header call EARLIER in the code, so that it executes WAY before anything else. Then, of course, you'd want to stop execution (since you're asking for a redirect.)

HOW A BROWSER WORKS

To understand why this is a problem -- why you can't send a header after sending content (like HTML), think of it how the browser sees the packet:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 28

<div><span>text</span></div>

The header is the first three lines. After that, two carriage returns and then the body. By sending html first, it forces php to calculate headers (including, most importantly, content length!) After that, the headers are sent. You're now on to sending body. So, by calling header() later, to add another header, you're asking php to go back in time and unsend the headers, which of course... it can't do.

Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
  • Okay understood. But, again, I got numerous other tags in the file, div and span and li and all...but they are not effecting the execution of the file. I mean if I remove those four lines, header() is working. How adding specifically those lines is effecting header() ? The thing is the files are interdependent to each other so if I move some code, some other code will lead to error. – bayblade567 Dec 10 '14 at 18:37
  • Are you sending those tags *after* calling `header()`? That's ok to do... – Lynn Crumbling Dec 10 '14 at 18:38
  • @bayblade567 Let me put it this way -- look for the line that is including `stuffs.php` and **absolutely** do not send any tags before that line. – Lynn Crumbling Dec 10 '14 at 18:40
  • **This is the format::** I have **stuffs.php, which includes header.php in the beginning of the file**. Therefore, everything, every tags in **header.php** is coming before the header(). Sorry for being so dumb, but I can't help it. – bayblade567 Dec 10 '14 at 18:41
  • @bayblade567 You aren't dumb; you just need to understand the order. As far as fixing it, no matter what it was doing before, you need to change it to follow that one basic rule: never send content before making a `header()` call. If you change your code to subscribe to that rule, you'll find that it always works. – Lynn Crumbling Dec 10 '14 at 18:43
  • Is this as simple as moving the `include("header.php")` to the end of stuffs.php? – Lynn Crumbling Dec 10 '14 at 18:47
  • Heyy....thanx a lot....it works. Whatever i was doing before, though it was working, maybe out of luck or some calculation, it wasn't a good practice at all. I changed few things and made sure that header() wasn't executed after any write, now it is working. Phew. – bayblade567 Dec 10 '14 at 18:47
  • Excellent. Glad to hear it :) – Lynn Crumbling Dec 10 '14 at 18:49
1

From PHP's own manual (http://php.net/manual/en/function.header.php):

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
Phil Tune
  • 3,154
  • 3
  • 24
  • 46