Php Session


A PHP session variable is utilized to store data about or change settings for a client session. session variable holds data around one single client and is accessible to all pages in a single application.

A session creates a file in a temporary directory on the server where registered session variables their values are stored. This information will be accessible to all pages on the site during that visit.

A session closes when the client closes the browser or in the wake of leaving the site, the server will end the session after a foreordained timeframe.

A PHP session takes care of this issue by permitting you to store client data on the server for some time in the future (for example and so on.) However, meeting data is transitory and will be erased after the client has left the site. in the event that you need perpetual stockpiling, you might need to store the information in a database.

Starting a PHP Session

Before you can store client data in your PHP session, You should initially fire up the session. the session_start() work must show up before the tag.

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

Storing a Session Variable

<?php
session_start();
// store session data
$_SESSION['user_name']="john doe";
?>
<html>
<body>
<?php
//retrieve session data
echo "hi =". $_SESSION['user_name'];
?>
</body>
</html>

Destroying a PHP Session

A PHP session can be destroyed by the session_destroy() function. if you want to destroy a single session then you can use unset() function.

<?php
unset($_SESSION['user_name']);
?>
<?php
 session_destroy();
?>