PHP Sessions

PHP Sessions Tutorial


Sessions in PHP are used to store information on the server. Data is available on all the pages in an application, a session is the best way to keep the information used in many pages.
Session is used to temporarily store and pass information from one page to another, until the user closes the website, the information is stored in the user's computer through a cookie, but the session Information is not stored on the user's computer.

Session works by creating a unique number for each visitor and for storage based on its ID. Session also helps in preventing the data of two users from getting confused with each other while visiting the same webpage.
To start a PHP session, we call from the session_start() function, this function first checks whether a session is already started or not, if no one has started before then it starts a session.

For Example :

We Create a file (session1.php) :

 

Output :Session variables are set.
 
Create another file name is (session2.php)

Output :Favorite color is yellow.
             Favorite animal is Dog.

 

: