Can anyone help me on how to start and end session using PHP? I really don’t have an idea on how to do it. Is there any function to do this? Please help.
How to Start and End Session
Dear friend,
-
Session holds the session variable information and it is very easy to create session in PHP.
-
Before you create a session variable in PHP you need to start the session which is accomplished by below function.
-
Session_start();
-
You have to use this function before you create session variables.
-
Now you can create session variable and assign value to session variable as below.
-
$_SESSION[‘variable name’]=value;
-
You can retrieve and print session variable value as below.
-
$_echo “session variable value”.$_SESSION[‘variable name’];
-
Use can check variable value is set or not using isset as below.
-
If isset($_SESSION[‘variable name’]){}
-
If you want unset the session variable or free the session variable unset method is used as below.
-
Unset($_SESSION[‘variable name’]);
-
Example
<?PHP
Session_start();
If(isset($_SESSION['userid']))
$_SESSION['userid']=$_SESSION[' userid ']+1;
Else
$_SESSION[' userid ']=1;
Echo "Views=". $_SESSION[' userid '];
//Unset session using
//Unset($_SESSION[' userid ']);
?>