It start new or resume existing session
<?php
session_start();
$_SESSION[‘lan’]=”PHP Code”;
echo $_SESSION[‘lan’] ;
session_destroy();
?>
It start new or resume existing session
<?php
session_start();
$_SESSION[‘lan’]=”PHP Code”;
echo $_SESSION[‘lan’] ;
session_destroy();
?>
Sessions are global variable that can store values and will be accessible throughout the site .The applications that uses session are user login,cart etc . Session values are stored in the server and it will will have reference to a an session id which will be generated when some one call session_start() and will be stored in the local machine
Once the session id created , this id will be passed to the server along with request and with the help of this the code can fect the values stored in the server .
Session values can be managed with the help of $_SESSION[] .
Session can be destroyed using the function session_destroy()
Session values will get destroyed if the browser is closed .
Example
<?php
session_start();
$_SESSION[‘lan’]=”PHP Code”;
echo $_SESSION[‘lan’] ;
session_destroy();
?>
You can delete / Destroy session using the PHP function unset()
Example
<?php
session_start();
if(isset($_SESSION[‘phpcodez_views’]))
unset($_SESSION[‘phpcodez_views’]);
?>
Also its possible to completely delete the data using the function session_destroy ()
Example
<?php
session_destroy();
?>
Before using a session variable ,we must check if that variable exists in $_SESSION array and that can be done using the function .
Example
<?php
session_start();
if(isset($_SESSION[‘phpcodez_views’]))
$_SESSION[‘phpcodez_views’] = $_SESSION[‘phpcodez_views’]+ 1;
else
$_SESSION[‘phpcodez_views’] = 1;
?>
When you want to store information in a session use $_SESSION – this is where you store and retrive session data .
Example
<?php
session_start();
$_SESSION[‘phpcodez_views’] = 1; // store data
echo $_SESSION[‘phpcodez_views’]; //retrieve data
?>
Before you begin storing information in PHP session , you must start the session and it must be mentioned at the very beggining of your code .
<?php session_start(); // start PHP session! ?>
Session variables are used to store values that are required across multiple web pages .
A website can not remember anything about the users those who have already visited the site since the HTTP request does not have state . But at time , it is necessary to remember some details(eg should understand if a user is logged in or not ) . In such case we can use session that enable the website to store the information for later use .