Saturday, October 7, 2017

PHP Session For State Management



What is a session in PHP?

  1. PHP session is used to store and pass information across multiple page.
  2. PHP session technique is widely used in shopping websites and other secure pages where we need to store and pass important information e.g. username, secure token ,product code, product name, product price etc from one page to another.
  3. PHP session creates unique ID for each browser session to recognize the user and avoid conflict between multiple browsers.
  4. Mostly PHP Session is used in User Login Page.

Where the session is stored in PHP?

A session variable's content is stored on the server, however the session is identified by a session ID which is stored at the client and sent with each request.

NOTE : In case of cookies all the session information's are store in local machine, So PHP session is way for secure than cookies. Best practice is always use session wherever require.

How to Start a PHP Session

Steps to start session and set the value in session variable.
  1. session_start() function is used to create session and start session.
  2. Session variables are set with the PHP global variable using $_SESSION.
Lets see the below to build more understanding on PHP session variable:

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set the session variables
$_SESSION["userid"] = "10323223";
$_SESSION["username"] = "skptricks";
echo "Session variables are set.";
?>

</body>
</html>

How to Get PHP Session Variable Values

In above example we have already set the session variables, now what we do is retrieving the session variables. Lets see the below example to get the stored session variables.
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables that were set on previous page
echo "Unique user ID is " . $_SESSION["fuserid"] . ".<br>";
echo "Username is " . $_SESSION["username"] . ".";
?>

</body>
</html>
Output:
-------------------------------
Unique user ID 10323223
Username is skptricks

How to Modify a PHP Session Variable

Suppose we have already set the session variable name, now the require is to set the new value in existing session variable. Lets see the below example in which we are modifying or overriding the existing session variable "username", what we have created in above step.

To modify the session variable, just assign the new value to session variable.

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

<?php
// to change a session variable, just overwrite it 
$_SESSION["username"] = "sumit kumar pradhan";
print_r($_SESSION);
?>

</body>
</html>


How to Destroy a PHP Session

To remove all global session variables and destroy the session, use session_unset() and session_destroy()
use session_unset() : Remove all session variables
session_destroy() : Destroy the session

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

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

// destroy the session 
session_destroy(); 
?>

</body>
</html>

Storing multiple values in session array and retrieving[NEW]

We can store array in session variable and later on retrieve data as per the need. In this example we are storing data in array and storing the all array values in session

Storing the value to session.
<?php

session_start();

//Get unique id from database or else generate id and store in below array
$userDetails = array (
    'username' => "sumit kumar pradhan...",
    'uid' => "8923002",
    'uemail' => "sumit@gmail.com"
);

// create session array
$_SESSION['userDetails'][] = $userDetails;

?>

Retrieving the value from session.
<?php

session_start();
// get the value from session
foreach ($_SESSION['userDetails'] as $item) {
    echo 'Username: ', $item['username'], '<br />';
    echo 'Uid: ', $item['uid'], '<br />';
    echo 'Email: ', $item['uemail'], '<br /><br />';
}

?>

No comments:

Post a Comment