Last updated on January 17th, 2023 at 03:03 pm

 

One of the main issue in PHP that we see quite often “Header Already Sent” warning. Deal with it through an easy approach. In this tutorial we will be discussing about how to solve “Header already sent” error and other use of Header function in PHP. The error you might see will be “cannot modify header information headers already sent by php”

Table of Contents

Debugging

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

Some times when you integrate an external website or sometimes when try to add something like this in a PHP page

<?php
require_once('body.php');
?>

Let us assume inside that body.php you have

<?php
session_start();
?

then most probably you will be getting a warning stating that
Warning: session_start(): Cannot send session cookie headers already sent
To eliminate this warning it is pretty simple just add this code in the body.php page

<?php
ob_start();
session_start();

<!--YOUR INCLUDE SCRIPT HERE-->

ob_end_clean();
?>

That being said, using the Header() function you can do the following,

1) Redirect your user to some other page.
2) Tell the browser not to cache your files
3) Content Disposition.

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.example.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.example.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.example.com');
?> 

This code generates a 301 status and hit count of “www.example.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" );
header("Refresh: $sec; url= http://www.mistonline.in" ); 

Force the browser not to cache the pages.

<?php
// Date in the past , tells 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" );
?>

Content Disposition / Content-type

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' );
?>

One thought on ““Header already sent” and other use of Header function in PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *