CodesJava

Easy learning with example program codes

PHP Session


PHP Session are the variables that stores information about a single user. These variables are used to hold the information and to pass the information temporarily from one page to another page. In this way, the informations stored by the Session variable are available to multiple pages of an application. This whole process is based on the user-key which is different for different sessions and is stored in the user’s computer. A session is accessed on another page, only if there is a match for the user-key. Otherwise, a new session starts.

To Start a Session:
PHP session_start() function is used to start a session.
Syntax
bool session_start ( void )
To Set and Get a Session Variable:
$_SESSION is a global variable which is used to set and to get the value of a session variable.
To Modify a Session:
$_SESSION variable can be used to set the value of a Session variable again, if any modification is needed.
To Destroy a Session:
PHP session_destroy() function is used to destroy a session.
To Remove the $_SESSION variables:
PHP session_unset() function is used to remove all the global session variables.
Example: Program to set and retrieve a Session variable value

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
 
<?php
// to change a session variable, just overwrite it
$_SESSION["flower"] = "lily";
$_SESSION["fruit"] = "apple";
echo "Favorite color is " . $_SESSION["flower"] . ".<br>";
echo "Favorite animal is " . $_SESSION["fruit"] . ".";
?>
 
</body>
</html>

Output

Favorite color is lily.
Favorite animal is apple.
Posted in PHP   


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap