Sessions are simple arrays used to store some information and the values can be used throught the project / website.

In PHP, Usually all variable values will be erased at the end of the script / output sent to browser. When you request the same page next time, the values will not be there. So when we want to store the values and want to use the values between the requests then we can use $_SESSIONS

Sessions are started when we call  session_start() . A unique id generated when session start called and this id will be sent to browser as cookie (PHPSESSID), Later the session id stored in the cookie used to retrieve stored information.



echo $_SESSION['name'];
Note: session_start() call should be the first line of your script. The reason behind this is, Cookies are sent to the browsers through headers. As session id stored in cookies we must call this session_start() as the first line, so it will be sent along with other headers else it will generate a warning.

Session Expiry:
Default session expiry time is around 24 miniutes, if the time between first request and the next exceeds this time then the values are erased. We can see how to extend this default session time in another article.


Comments (0)
Leave a Comment

loader Posting your comment...