Back to Top

Session_start( ) :: At the top of the page

Today I am going to explain simple but tricky interview question. Before Some days, one of my students asked me “why we need to add session_start() functions at the first line of code?” question and want to share my answer here

Web Server sends the response to the web client and browser renders the HTML information of the page.so Before the HTML information rendered into a screen and display content to the screen, all header information like cache the page set by header r not,require to execute at the beginning of the page because once something print or body tag is executed, no more headers can be sent.

And the setcookie() and session_start() functions add headers to the response. So we must have to add both functions before any output or any HTML rendering starts.

If any print/echo statement used before setcookie( ) or session_start( ) function is called, the PHP generate an error message that will be seems like this:

Above error says that file contain print statement before you sends a header so If you find “headers already sent” error message, make sure that there are no print/echo statements before you call setcookie( ) or session_start( ) functions.

You may also like:

Session In Magento
SESSION SAVE PATH in PHP
Auto start Session in PHP

I hope this small article will help you. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Leave a Reply

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

Most Popular Posts

How to use Flickr API with PHP

Posted on 5 years ago

Bhumi

How to Get local time using EarthTool

Posted on 5 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 2 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 3 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 4 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 4 years ago

Bhumi