PHP Sessions

PHP sessions are a way to store information (variables) to be used across multiple pages.


Unlike cookies, which are stored on the client-side (in the browser), session data is stored on the server.


This allows for better security, as sensitive information isn't exposed to the client-side (browser).

Start a PHP Session

To use sessions in PHP, you need to call session_start() at the beginning of the script. This function must be called before any output (HTML, text, etc.) is sent to the browser.


Now, let's create a new page called "demo_session.php". In this page, we start a new PHP session and set some session variables:

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["username"] = "JohnDoe";
$_SESSION["email"] = "john.doe@example.com";
echo "Session variables are set.";
?>
</body>
</html>

Get PHP Session Variable Values


Next, we create another page called "get_session.php". From this page, we will access the session information we set on the first page ("demo_session.php").


Notice that session variables are not passed individually to each new page, instead they are retrieved from the session we open at the beginning of each page (session_start()).


Also notice that all session variable values are stored in the global $_SESSION variable:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo "Username: " . $_SESSION['username'] . "<br>";
echo "Email: " . $_SESSION['email'] . "<br>";
?>
</body>
</html>

Another way to show all the session variable values for a user session is to run the following code:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION);
?>
</body>
</html>

Modify a PHP Session Variable


Modifying a session variable in PHP is simple. After starting the session using session_start(),


you can change the value of any session variable directly by assigning a new value to it.

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// to change a session variable, just overwrite it
$_SESSION["na"] = "yellow";
print_r($_SESSION);
?>
</body>
</html>

Destroy a PHP Session


Destroying a session in PHP involves removing all session data and optionally removing the session cookie. The process includes two main steps:

  1. Unset individual session variables.
  2. Destroy the entire session.

This ensures that the session is completely destroyed, and no session data remains accessible.

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>
</body>
</html>

Note

Use session_unset() when you want to clear session data but keep the session alive (e.g., removing user data after a form submission).


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.