Ad

PHP Session

php session


What is the PHP Session

PHP sessions enable you to save stateful information between page requests from the same user by storing user data on the server for later use. Sessions are frequently used for things like maintaining a user's login across several website pages without requiring a new authentication every time a page loads.

Sessions are essential to web development because they preserve stateful behavior in the stateless HTTP protocol. They let websites to retain user information and preferences, including as login status, contents of the shopping cart, and user data, across pages. A session ID, which is often sent between the client and server via URLs or stored in a cookie, is used to maintain sessions.

This data is safely stored in the server-side session storage, guaranteeing that only authorized users can access it and that neither other users nor outside threats may access it.

Information is briefly stored and sent across pages using PHP sessions (until the user closes the website).In order to retain and transfer cart information, such as username, product code, product name, and price, among other details, from one page to another, shopping websites frequently employ the PHP session approach.In order to identify the user and prevent conflicts across different browsers, PHP sessions generate distinct user IDs for each browser.

When users move between pages or take activities on the application, web applications frequently need to retain user-specific data. PHP sessions offer a workaround for this by enabling the temporary storing of data across several pages while a user is on the site. A distinct session ID is given to each visitor; this ID might be sent over the URL or kept in a cookie. By using this session ID, the server may obtain the relevant session data and provide users with individualized experiences. In order to provide dynamic and customized online experiences, sessions are necessary for storing user-specific data, monitoring preferences, and enabling login sessions.


1] PHP Session_start()

Start a PHP session: The first step is to start a session. Session variables can be created to store data after the session has started. PHP session_start() function is used to start a new session.

Syntax :- 

<?php 

session_start();

?>

Example :-

<html>

<body>

<?php

$_SESSION["user Name"]="seeta";

echo "Session Information set the successfully.";
?>
<a href="session2.php"> Next page visit</a>

<?php

session_start();

?>

</body>

</html>


Output 

2] PHP Access  Session

Next, create another page called “demo_session1.php”. This page accesses session information established on the first page (“demo_session2.php”). Session variables are not passed individually to each new page, but are retrieved from the session that opens at each page start (session_start()).


Syntax :- 

session_start();

<?php
session_start();


$username = $_SESSION['username'];{
$userId = $_SESSION['user_id'];}

echo "Welcome back, $username (ID: $userId)";
?>


3] PHP Session_destroy

PHP execution can be terminated using the session_destroy() function, which requires no parameters.

<?php

session_start();

if(isset($_SESSION["name"])){
unset($_SESSION["user_id]);}
}

session_destory();

?>

Related Post :-

No comments

Powered by Blogger.