Open In App

PHP | Sessions

What is a session?

In general, session refers to a frame of communication between two medium. A PHP session is used to store data on a server rather than the computer of the user. Session identifiers or SID is a unique number which is used to identify every user in a session based environment. The SID is used to link the user with his information on the server like posts, emails etc.



How are sessions better than cookies?

Although cookies are also used for storing user related data, they have serious security issues because cookies are stored on the user’s computer and thus they are open to attackers to easily modify the content of the cookie. Addition of harmful data by the attackers in the cookie may result in the breakdown of the application.
Apart from that cookies affect the performance of a site since cookies send the user data each time the user views a page. Every time the browser requests a URL to the server, all the cookie data for that website is automatically sent to the server within the request.



Below are different steps involved in PHP sessions:

Important Points

  1. The session IDs are randomly generated by the PHP engine .
  2. The session data is stored on the server therefore it doesn’t have to be sent with every browser request.
  3. The session_start() function needs to be called at the beginning of the page, before any output is generated by the script in the browser.

Article Tags :