0

What is session in php, how to start and stop session

phpimg

Session in PHP

Session is a way to store the user information(in variables) to be used across multiple pages.

For example, When we work with an application, we may open it, do some modification and we close it. This is what a session is.The computer may know who you are, and when you start and end the application. But in internet there was a problem in web server , it doesn’t know who you are and what you do.Because of the HTTP, it doesn’t maintain the state information.

Session variable solve this problem by storing user information.Session variable starts once the user open the application and its last until the user close the browser. Hence, session variable holds the information about a single user, and maintain through out the application.

Start the session

To start a session variable, use session_start() function.

Session variables are set with PHP global variable $_SESSION

Here is a code to start a session with session variable

<?php
session_start();
?>
<html>
<body>
<?php
//to set a session variable
$_SESSION[“msg”]=”Working with session”;
echo “Session started”;
?>
</body>
</html>

Stop the session

To stop a session variable, use session_unset() and session_destroy() functions.

  • session_unset is used to remove all session variables.
  • session_destroy is used to destroy the session.

Here is a code to remove the session variable and destroy the session

<?php
session_start();
?>
<html>
<body>
<?php
session_unset();
session_destroy();
?>
</body>
</html>

Filed in: PHP

Recent Posts

Bookmark and Promote!

Leave a Reply

Submit Comment
© 2017 Expert Shout. All rights reserved. XHTML / CSS Valid.