Session is a way to store information across multiple pages in a website.
How to start a session:
A session can be started by using session_start() function.We can set session variable by using PHP global variable $_SESSION.
consider an example:
create a new page demo1.php and start a new PHP session in this page and set some session variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php // Start the session session_start (); ?> <!DOCTYPE html> <html> <body> <?php // Set session variables $_SESSION ["username"] = "sowmya"; $_SESSION ["email"] = "sowmya.vejerla@gmail.com"; echo 'username : ' . $_SESSION ['username']; echo '<br>'; echo 'email : ' . $_SESSION ['email']; ?> </body> </html> |
Generally session variables can't be accesses directly so we need to start the session again in this page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php // Start the session session_start(); ?> <!DOCTYPE html> <html> <body> <?php //access session variable echo $_SESSION['username']; echo '<br>';//update session variable $_SESSION['email'] = "vejerla.sowmya@gmail.com"; echo $_SESSION['email']; //unset a session variable unset($_SESSION['username']); //remove all session variables session_unset(); //destroy the session session_destroy(); ?> |
Outputs:
![]() |
| demo1 output |
![]() |
| demo2 output |
This is how session will be implemented. Session is mostly used for storing user-specific data for a limited period of time (for example across user login to logout) namely a single session.
Hope you liked this post.If yes , please share it to your friends and subscribe to new PHP solutions.Thank you :)


Comments
Post a Comment