Session in PHP

What is the meaning of session in 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 values 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.

A session is used to store the information on the server rather than client computer(browsers) In simple words- First of all, you have to start the session on every single page. If you want to get the value on the next page(like username and another information related) for that session help you.

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

The function session_start() first checks if the session is already started and if the 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[].

Start a PHP session

As we discussed above , you need to start the session on single page. The session is started by the session_start() function. After started the session variable can store the information. The following PHP session start code will help you understand the concept.

<?php
  
session_start(); 
  
?>

PHP Session variable

The session variable is used to store the information. For example - A user can store all information in variables and access across the website pages. You need to set session variable values. Session variables are set with the PHP global variable:-

    
$_SESSION

In the previous tutorial, we have discussed the PHP $_POST variable and $_GET variable. These are related to PHP POST method and PHP GET method . These are two form handling method. They handle the form data. $_session is a part of the server. You need to set the $_session variable first.

How to store session data ?

One of the best things is to store session data in the session variable $_SESSION Session data in key-value pairs using the $_SESSION[] superglobal array. We store session data in a key-value pair. We use $_SESSION[] superglobal array to store session data. You can access these variables data across the website pages. The session data can be access during the session life. After the session destroyed, you can not access session data. Let's create three PHP session variables and set. Create a page page1.php and use this code.

<?php
//start session in every single page
session_start();

?>

<!DOCTYPE html>
<html>
<body>

<?php
// Set three session global variables 
$_SESSION["username"] = "admin";
$_SESSION["name"] = "Saim";
$_SESSION["dept"] = "IT";
?>

</body>
</html>


Get PHP session global variables values

You can get session variables values by start a session. First of all, you need to start the session at the beginning of the page and access all variables data. The following PHP code helps you access session variable data easily. Create a new page page2.php and execute the code below.

<?php
//start session in every single page
session_start();

?>

<!DOCTYPE html>
<html>
<body>

<?php
echo 'Welcome:' . $_SESSION["username"] . '
'; echo 'Your name is ' . $_SESSION["name"] . '
'; echo 'Your department is ' . $_SESSION["dept"]; ?> </body> </html>

Output

  Welcome:admin
Your name is Saim
Your department is IT  
    
In this example, we set variables at page1.php and access the values of the variables on another page page2.php. This is the main advantage of the session.

PHP session destroyed & Remove

Session destroy is a process for removing all global variables.You can remove and destroy complete PHP sessions using session_unset() and session_destroy() functions. After sessions destroyed, the unknown person cannot access the session data. This is a restriction for an unknown person. Just destroy your session and out. No one can access PHP session data.

<?php
//start session in every single page
session_start();

?>

<!DOCTYPE html>
<html>
<body>

<?php
// remove all session variables
session_unset(); 

// destroy the session 
session_destroy(); 

?>

</body>
</html>


PHP Login using session

In the previous tutorial, we created a login form in PHP without session. We can use the session with the login form. A user is login during the session life. After session destroyed, unknown user cannot login and access user profile. An authenticated user can access the information through re-login. The session is also used to the security purpose for user's accounts.

A simple example of login page in session .

PHP Start a session .

Login using Session

<?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 the above example, the session started at the beginning of the page. In the above PHP login code, we used the session. When a user destroyed the sessions, unknown users never log in.

Session destroyed

Session destroyed means you want to logout (destroyed) the session. When 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 the above example, first of all, the session has started and then the session was destroyed. If the session is destroyed the user will redirect to the main login page where the user can log in again just put id and password. With the help of other examples.

Session Destroyed

session_destroyed() function is used to destroy a session. The function does not require 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 destroy a single session variable.

The example of single session unset .

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

PHP session modify

To modify a session you have to overwrite it .

Example

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

<?php

$_SESSION["username"] = "admin123";
print_r($_SESSION);
?>

</body>
</html>
Run

Session Example

Login with session using PHP and MYSQL database

Recommended Posts:-

Previous Posts:-