PHP: Session | Session start | PHP Sesion Destroy

Techno PHP

The session is used to access the various pages of an entire website. A session creates a temporary directory on the server. With the help of session variables and their value are stored .

The data will available to all pages on the site. If you are working on an application you need to start it and destroy it.

In simple words- First of all, you have to start the session and destroy the session. If you want to get the value on next page(like username on another page) for that session will help you.

A PHP session is normally starts by making a call to the session_start() function .

The function session_start() first checks if session is already started and if session is not started then it starts one .

It is mandatory to start the session on every page. The session starts at the beginning of the page. The session variables are stored in an associative array called $_SESSION[]. A simple example of login page in session .

PHP Start a session .

session_start();

<?php
session_start();//Session Start
$cser=mysqli_connect("localhost","root","","database_Name");

if(isset($_POST['sub']))
{
	$a=$_POST['email'];
	$b=$_POST['password'];
	
	$res=mysqli_query ($cser,"SELECT* from regis where email='$a' and password='$b'");
	$result=mysqli_fetch_array($res);
	if($result)
	{
		$_SESSION['email']=$a;
		echo "you are login";
	    header('location: welcome.php');
	
	}
	else 
	{
		echo "failed" ;
	}
}
?>


In above example session started at the beginning .

Session destroyed .

Session destroyed means you want to logout (destroyed) the session . When a user login and at the end of his work he want to logout then the session destroyed will help you .



session_destroy();

<?php
session_start ();
session_destroy();

header("location:index.php")
	


?>

In above example first of session started and then session session destroyed . If the session is destroyed the user will redirect to the main login page where the user can login again just put id and password. With the help of other examples.

Session Start:-

In the below example starts a session at the beginning of a page and register a variable call count . The count variable will increment each time when the user is visited page during the session . Now we will use the isset () function to check if the session is already set or not.

Session Example

<?php
session_start();//Started session 
if( isset( $_SESSION['count'] ) )
{
$_SESSION['counter'] += 1;
}
else
{
$_SESSION['count'] = 1;
}
$msg = "You have visited this page  ". $_SESSION['count'];//Counts value 
$msg .= "in this session.";
?>
<html>
<head>
<title>PHP Session  </title>
</head>
<body>
<?php echo ( $msg ); ?>
</body>
</html>
Run

Session Destroyed

session_destroyed() function is used to destroyed a session . The function does not required any type of arguments and the single call destroyed the session variable.
You can use unset() function to unset a session variable . You can use unset() function to destroyed a single session variable .

The example of single session unset .

Session Example

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

PHP Modify session . To modify a session you have to overwrite it .

Example

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

<?php

$_SESSION["favcolor"] = "yellow";
print_r($_SESSION);
?>

</body>
</html>
Run

The example of destroyed the all session .

Related