facebook twitter youtube
in PHP - 02 Jan, 2009, This Tutorial Has Been Viewed 28,653 Times.
by admin - one comment
“Header already sent” in php error and other use of Header function in PHP
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)


PHP main issue Header Already Sent Warning.Deal with it through an easy approach.

“Header already sent” error and other use of Header function in PHP

Useing the Header() function you can do the following things
1) Redirect your user to some other page.
2) Tell the browser not to cache your files
3) Content Disposition.
“Header already sent” error

Most of the PHP learner (including me) has spent hours in debuging this problem. This problem happens while using session variables. While using session variables you must initialize the session using the function session_start(), and the problem occurs here.
This problem has got an unbelievebly simple solution, just start the PHP block (where you have written the session_start() at the very first line of the page.
To elastrate this

is OK. But

is wrong.

Again , you can not even leave a blank line above the PHP code as it is considered as HTML code

As you can see there is a blank line above thePHP block , this will raise error.
Another example ,
echo ‘Hello’;
will give error as the statement “echo ‘Hello’ output an HTML on the page before “session_start();” is executed .
So , the bottomline is do not output anything before the session_start() is executed.

1) Redirect your user to some other page.
You can redirect you user to some other page using the code “Location” parameter of header() function.

header ( “Location: http://www.koderguru.com/” ); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
?> This code will redirect your user to the page http://www.example.com.
Warning : The statement
“header ( “Location: http://www.example.com/” );”
does not generate 302 response, that mean the page will be redirected but it will not be counted as a hit to “www.koderguru.com”, so the target page looses a hit count. To avoid this use the following code.
header (‘HTTP/1.1 301 Moved Permanently’);
header (‘Location: http://www.koderguru.com’);

This code generates a 301 status and hit count of “www.koderguru.com” will increase.

Also , you can redirect after some interval , using the code
// Redirects the browser after $sec seconds
header(“Refresh: $sec; http://www.mistonline.in” );
should be:
header(“Refresh: $sec; url= 

2) Force the browser not to cache the pages.

You can do this with the code
// Date in the past , tels your browser the cache has already expired
header ( “Expires: Wed, 06 Jul 2006 05:00:00 GMT” );

// Here the line tells the browser that the last modification date of this page is right now
//So it must reload the page
header ( “Last-Modified: ” . gmdate ( “D, d M Y H:i:s” ) . ” GMT” );

// HTTP/1.1
header ( “Cache-Control: no-store, no-cache, must-revalidate” ); // do not cache/store this page
header ( “Cache-Control: post-check=0, pre-check=0″ , false );

// HTTP/1.0
header ( “Pragma: no-cache” );
3) Content Disposition.

You can also generate different type of content other than html , using Header() function

// We’ll be outputting a PDF
header ( ‘Content-type: application/pdf’ );

// It will be called downloaded.pdf
header ( ‘Content-Disposition: attachment; filename=”downloaded.pdf”‘ );

// The PDF source is in original.pdf
readfile ( ‘original.pdf’ );

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

Incoming search terms:

coded by nessus

Leave a Reply

  • //12 Mar 2009


    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
Please support the site
By clicking any of these buttons you help our site to get better