Describe i) Start session ii) Get session variables
PHP session_start() function is used to start the session. It starts a new or resumes existing session. It returns existing session if session is created already. If session is not available, it creates and returns new session
Syntax 1. boolsession_start( void )
Example 1.session_start();
PHP $_SESSION is an associative array that contains all session
variables. It is used to set and get session variable values.
Example: Store information
2. $_SESSION["CLASS"] = "TYIF STUDENTS“
Example: Program to set the session variable (demo_session1.php)
<?php
session_start();
?>
<html>
<body>
<?php
$_SESSION["CLASS"] = "TYIF STUDDENTS";
echo "Session information are set successfully.<br/>";
?>
</body>
</html>
ii)Get Session variables
We create another page called "demo_session2.php". From this page,
we will access the session information we set on the first page
("demo_session1.php").
Notice that session variables are not passed individually to each new
page, instead they are retrieved from the session we open at the
beginning of each page (session_start()).
Also notice that all session variable values are stored in the global
$_SESSION variable:
Example:- program to get the session variable
values(demo_session2.php)
<?php
session_start();
?>
<html>
<body>
<?php
echo "CLASS is: ".$_SESSION["CLASS"];
?>
0 Comments