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
<?php
session_start();
?>
is OK. But
<HTML>
<?php
session_start();
?>
is wrong.
Again , you can not even leave a blank line above the PHP code as it is considered as HTML code
<?php
session_start();
?>As you can see there is a blank line above thePHP block , this will raise error.
Another example ,
<?php
echo ‘Hello’;
session_start();
?>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.
<?php
header ( “Location: http://www.koderguru.com/” ); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?> 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.
<?php
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= http://www.mistonline.in” );
2) Force the browser not to cache the pages.
You can do this with the code
<?php
// 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
<?php
// 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’ );
?>
Incoming search terms:
- functional inurl:/register intext:upcoming intext:published intext:submit -inurl: php (9)
- header already sent error in php (9)
- Address already in use: AH00072: (6)
- pictures inurl: /a/viewtopic php?t= (6)
- publishing inurl:/forumdisplay php? (6)
- how to prevent header alreay sent in php (5)
- member php?u= error (5)
- exhale inurl:/forumdisplay php? (5)
- inurl:/showthread php?page=1857 (4)
- javascript send headers (4)
- abscissae inurl:/showthread php?p= (4)
- years inurl:/guestbook php?page= (4)
- how to avoid header already sent in php (3)
- header function in php (3)
- What is use of header() function in php ? (3)
- listbox example yii (3)
- how to handle headers already sent error in php (2)
- php prevent header already sent (2)
- header already sent php error (2)
- javascript urlreferrer (2)
- header already sent when invoke rediderct function in wordpress (2)
- mpdf header error (2)
- inurl:_forum_show php?no_cache=0 (2)
- inurl:viewtopic php (2)
- header() function in php (2)
- headers already sent error in php (2)
- inurl:/users/index php username real name registration date last login -e (2)
- music inurl: board/showthread php?t= (2)
- php generate PDFfrom database sample source code (2)
- how to avoide error header already sent (2)
- what is use of header function in php (2)
- apache headers already sent error (2)
- forum inurl: chat/member php?u= (2)
- wordpress header location error (2)
- mpdf error checking (2)
- php 5: redirect a page not use header (1)
- php back button session (1)
- using php to send variables in header statement (1)
- php curl_init error headers already sent (1)
- usin g the location funtion in php (1)
You will also be interested in ,
- Set xampp:- Apache Mysql Filezilla Mercury Tomcat as a windows service and get xampp to start automatically on boot up
- Append data to a text file using php
- Zip or Archive a directory using php
- Water mark images using PHP 5 and GD Library
- Image gallery using php and mysql blob storage and displaying the image from mysql blob
- FPDF error: Alpha channel not supported error message
- Warning: session_start(): Cannot send session cookie headers already sent
- Simple Visitor Counter Using php
- Autosuggestion / Autocomplete script using PHP and Ajax


cool….