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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use HTML5 download attribute?

Posted on 9 years ago

Bhumi