How to use php session in 2022

How to use php session in 2021
How to use PHP session in 2021

PHP session is a variable that stores all the values of user input.

Session: A session typically starts when a browser requests a web page from a server. The server assigns the browser an id and saves this id in the session's "sid" cookie. The server then responds to the request by sending back a response containing an encrypted version of the "sid" cookie. 

In PHP, this information is stored in $_SESSION[‘sid’];

For you to fetch your current session ID from PHP, you need to type in $_SESSION['sid'];

Session in PHP: A Session is used to store data or information in a variable that can be used anywhere in the project. Session data is not stored on the local computer. 

Starting a PHP Session: A session_start() is a predefined function that is used to start a session. It is recommended to put the call to session_start() function at the beginning of the page but not compulsory. $_SESSION[]is a superglobal variable that is used to store session variables and it acts as an associative array.

How to use session in php?

page1.php

<?php

session_start(); 

$_SESSION['name']="Icoderweb";

$_SESSION['marks']=380;

$_SESSION['course']="MCA"; ?> 

How to Getting Session Variable Values in PHP?

page2.php 

<?php                       

session_start();

?>

<html>

<head>

<title>ICODERWEB</title>

</head>

 <body>

<h1>Student Details</h1>

<h1>Student Name:<?php echo $_SESSION['name']; ?></h1>

<h1>Student Marks :<?php echo $_SESSION['marks']; ?></h1>

<h1>Student Course:<?php echo $_SESSION['course']; ?></h1>

</body>

</html> 

*****OUTPUT*****

Student Details

Student Name:Icoderweb

Student Marks :380

Student Course:MCA

Destroy session: session_destroy() function is used to destroy all the sessions variable. 

How to destroy session in PHP?

<?php

session_start();

session_destroy();

?>


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.