Solved

Warning: Headers already sent...

Posted on 2010-11-30
31
454 Views
Last Modified: 2012-05-10
Getting this warning on my site at the moment. I know this usually means that I tried to output something to the browser before I started the session but that doesn't really make any sense here. The site worked fine yesterday and all I've changed since then on the page is a few letters in the title.

Other things I've done are
Directed my host to upgrade my hosting package
Put a 301 redirect on a different website that points to this page thats giving the warning

Here's the exact text of the warning:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php:1) in /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php on line 7

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php:1) in /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php on line 7

Here's the code it references:
<?php
require_once('php-assets/class-inputs.php');
require_once('php-assets/class-mortgage.php');
require_once('php-assets/class-user-defined-mortgage.php');
include_once('php-assets/class-page.php');
$doc_root = eregi_replace("\/web$", "", $_SERVER["DOCUMENT_ROOT"]);
ini_set("session.save_path", "$doc_root/_SESSIONS/");
session_start();

Open in new window


None of those required files should be sending anything to the browser. I've checking for whitespace at the ends of those lines and there is none. Could it be the hosting upgrade I just ordered?
0
Comment
Question by:duckyfuzz
  • 16
  • 6
  • 3
  • +3
31 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34238225
the error is very clear:
> headers already sent by (output started at /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php:1) in /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php on line 7

so, please show the first 8 lines of that file.
you surely have some echo/print or implicit html return in there.
0
 
LVL 12

Expert Comment

by:enachemc
ID: 34238233
session_start() sends information in the HEAD

mortgage-calculator.php probably gives an error before this
so be sure to call session_start() before mortgage-calculator.php
0
 

Author Comment

by:duckyfuzz
ID: 34238252
Further info:

I downloaded the site down onto my localhost and it seems to work fine there. No warnings.
0
 
LVL 12

Expert Comment

by:enachemc
ID: 34238262
probably because your site is configured not to show warnings, so no warning is displayed in mortgage-calculator.php so session_start() can work fine
0
 
LVL 2

Expert Comment

by:rnevet
ID: 34238276
The error actually means that you are trying to add/change headers after you already started sending the response. It's enough that in one of the classes you are including you added a space that is written to the output.
0
 

Author Comment

by:duckyfuzz
ID: 34238290
That code snippet I posted is the first few lines of mortgage-calculator,php. Line 1 is simply
<?php

Open in new window

as you can see there.

The warning appears when I visit mortgage-calculator.php.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34238300
please post the 8 first lines, not just the first one.
0
 

Author Comment

by:duckyfuzz
ID: 34238356
@enachemc,

How can I check that warnings are being shown? My php error log on localhost has no errors logged today relating to this issue.
0
 
LVL 12

Expert Comment

by:enachemc
ID: 34238365
check the error reporting level in php.ini
0
 

Author Comment

by:duckyfuzz
ID: 34238376
@angelll,

This is the first 8 lines of mortgage-calculator.php:
<?php
require_once('php-assets/class-inputs.php');
require_once('php-assets/class-mortgage.php');
require_once('php-assets/class-user-defined-mortgage.php');
include_once('php-assets/class-page.php');
$doc_root = eregi_replace("\/web$", "", $_SERVER["DOCUMENT_ROOT"]);
ini_set("session.save_path", "$doc_root/_SESSIONS/");
session_start();

Open in new window



Sorry if I'm not explaining myself well, as you can tell I'm vary much a self thought amateur programmer. I tend to not know the industry standard ways of explaining things.
0
 

Author Comment

by:duckyfuzz
ID: 34238399
@enachemc,

error_reporting  =  E_ALL
display_errors = On
log_errors = On

Open in new window

0
 
LVL 12

Expert Comment

by:enachemc
ID: 34238409
just call session_start() on line 2 of your main php file
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34238418
what if you do:
<?php
require_once('php-assets/class-inputs.php');
require_once('php-assets/class-mortgage.php');
require_once('php-assets/class-user-defined-mortgage.php');
include_once('php-assets/class-page.php');
$doc_root = eregi_replace("\/web$", "", $_SERVER["DOCUMENT_ROOT"]);
session_start();
ini_set("session.save_path", "$doc_root/_SESSIONS/");

Open in new window

0
 
LVL 12

Expert Comment

by:enachemc
ID: 34238425
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
0
 

Author Comment

by:duckyfuzz
ID: 34238453
@rnevet:

I'm sure I haven't changed them since yesterday though so I don't see how they could be outputting anything today that they weren't yesterday. They're also a couple of thousand lines so I'd rather check that last!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:duckyfuzz
ID: 34238504
@enachemc:

I changed it to this:
<?php
session_start();
require_once('php-assets/class-inputs.php');
require_once('php-assets/class-mortgage.php');
require_once('php-assets/class-user-defined-mortgage.php');
include_once('php-assets/class-page.php');
$doc_root = eregi_replace("\/web$", "", $_SERVER["DOCUMENT_ROOT"]);
ini_set("session.save_path", "$doc_root/_SESSIONS/");

Open in new window


at your suggestion your suggestion.

I get almost the same warning:


Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php:1) in /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php:1) in /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php on line 2

The page is here if anyone wants to look: Mortgage Calculator. Click the 'wiew low bandwidth version' link at the bottom to get a pure html version.
0
 

Author Comment

by:duckyfuzz
ID: 34238515
@angellll:

In that case I get:


Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php:1) in /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php on line 7

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php:1) in /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php on line 7
0
 

Author Comment

by:duckyfuzz
ID: 34238549
The warning points to line number 1 as the code the outputted right? But line number 1 is just <?php. I checked for whitespace around that and there none. Could the offending line be somewhere other than the line the warning points to?
0
 
LVL 12

Expert Comment

by:enachemc
ID: 34238563
error_reporting  =  E_ERROR in php.ini
restart web server
0
 

Author Comment

by:duckyfuzz
ID: 34238575
@enachemc:

I changed the php ini to
error_reporting  =  (E_ALL ^ (E_NOTICE | E_WARNING));

Open in new window

on localhost. Restarted server and refreshed. Still no warning on the page when on localhost.
0
 

Author Comment

by:duckyfuzz
ID: 34238603
No warning with error_reporting  =  E_ERROR either.

Could it be that when I upgraded my hosting package, they changed my php version or something and now I have a compatibility problem?

As far as I knew all I was getting was a bit more space, bandwidth etc.
0
 

Author Comment

by:duckyfuzz
ID: 34238964
OK it looks like I fixed it by pushing through a development version of the site I had been working on. Seems ok now. Very weird issue though. Still don't know what the problem could have been.

0
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 34239038
I'm getting this error message on your site:

Warning: Cannot modify header information - headers already sent by (output started at /content/Hosting/m/o/mortgagemainframe.ie/web/mortgage-calculator.php:37) in /content/Hosting/m/o/mortgagemainframe.ie/web/php-assets/class-calculator.php on line 427

Can you upload the two files referenced in the message so we can look at them?

One quick thing to try would be to get rid of the ?> at the end of your .php files.  That eliminates problems with extra white space at the end of the files and php doesn't care if the closing tag is there or not.
0
 

Author Comment

by:duckyfuzz
ID: 34239175
@smadeira:

Hi, could you just go into private browsing or something and try again please. Just want to confirm there's still a problem in case you visited in the middle of me uploading the new version. I'm not getting any errors any more.

Thanks.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34239279
@duckyfuzz: I see there is a lot of message traffic on this, and not all of it is helpful.  Here are the rules about headers.  All headers must be sent and complete before any browser output.  Headers include cookies, and the session handler sets cookies.  That is why you want to put session_start() at the VERY TOP of your scripts.

Suppressing errors with error_reporting() is a sure recipe for confusion.  You WANT the errors, so you can see what needs to be corrected!

You might try using the output buffers.  Right at the top of your main script, and the only thing you ever put before session_start(), add this.

ob_start();

You might also want to put the name of the script file on the open-php line like this.  It will help you with your debugging, believe me.  Something like this...

<?php // duckyfuzz_mortgage_calculator.php
0
 

Author Comment

by:duckyfuzz
ID: 34239621
@Ray,

I've read before that there should never be anything before session_start(); and I was following this rule for a long time. The reason I started to abandon it was that I was having a problem a while ago where my scripts would try to use function which were contained in classes which hadn't yet being properly included.

I asked on a message board about this and I was advised to move their inclusions above the session start to ensure that they were fully loaded before anything tried to use them. Ot seems now that this is not advisable.

Is there a different solution to this problem that I can use instead of putting the inclusions before session start (is that what output buffers are for? I'm going to read about them now).

Thanks for the file labelling tip.
0
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 34239711
On your low-bandwidth version of the site you still get an error message as I have posted above.  It displays right near the calculate button.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34240225
"... this is not advisable."

10-4.  The exception of ob_start() is a very useful exception!
0
 

Author Comment

by:duckyfuzz
ID: 34240386
Yeah you're right I got the same error.

I can identify the problem that's causing that issue though. I'm definitely outputting to the browser before I try to send that button. I'm not exactly sure what I can do about it though. I might start a new thread for that one.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34240435
Looking at what little bit we can see of your code, it might be time for this:
http://www.amazon.com/Refactoring-Improving-Design-Existing-Code/dp/0201485672

The eregi_replace() function has a large red warning label.
http://us.php.net/manual/en/function.eregi-replace.php
0
 

Author Comment

by:duckyfuzz
ID: 34240738
heh yeah I think I need to move on from the beginner php book I have all right. I was actually wondering where I might go next so maybe I'll check that book out.

The eregi_replace is there because my hosting company wont let me upgrade my PHP version to 5.3.

The second header sending problem mentioned above is basically because I'm trying to turn my web application into a framework (or something like that, I'm not actually sure what a framework is!) so that each field on the calculator is part of it's own self contained module and I can add or remove modules at a whim without breaking anything. That's basically where stand in my development as a programmer.

Essentially the problem is that the calculate button needs to test certain conditions and the redirect to the results page if everything is ok. But when I try to put the header(location ... ) inside a self-contained module, it is necessarily along side the calculate button which is half-way down the page. THis stuff is sent to the browser before I attempt to redirect.

I don't know if that makes any sense.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now