How to Set and Get Data from Session in PHP

1 year ago admin PHP

In this tutorial, we will see how to set a session, how to retrieve data from a session, how to update data in a session, and how to delete a session in PHP.


Set and retrieve a session in php

To set a session in PHP use the code below:

                                                        
                                                                                                                        
<?php
    // Start the session
    session_start();
?>
<!DOCTYPE html>
<html>
    <body>
        <?php
            // Set session variables
            $_SESSION["username"] = "john2023";
            $_SESSION["logged"] = "yes";
            // Get the session variables
            echo "Your username is " . $_SESSION["username"] . ".<br>";
            echo "You are logged in " . $_SESSION["logged"] . ".";
        ?>
    </body>
</html>

Update a session in php

To update a session in PHP we use the same code just change the values:

                                                            
                                                                                                                                
<?php
    // Start the session
    session_start();
?>
<!DOCTYPE html>
<html>
    <body>
        <?php
            // Set session variables
            $_SESSION["username"] = "john2024";
            $_SESSION["logged"] = "yes";
            // Get the session variables
            echo "Your username is " . $_SESSION["username"] . ".<br>";
            echo "You are logged in " . $_SESSION["logged"] . ".";
        ?>
    </body>
</html>

Delete a session in php

To delete a session we use session_unset() and session_destroy():

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

Related Tuorials

How to Sort Associative Arrays in Descending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the key...


How to Sort Associative Arrays in Ascending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the key v...


How to Sort Associative Arrays in Descending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the valu...


How to Sort Associative Arrays in Ascending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the value...


How Do you Sort an Array in Descending Order in PHP

In this lesson, we will see how to sort descending an array in PHP, we will use the rsort() function...


How to Sort Ascending an Array in PHP

In this lesson, we will see how to sort ascending an array in PHP, we will use the sort() function t...


How to Remove a Key and its Value from an Associative Array in PHP

In this lesson, we will see how to remove a key and its value from an associative array in PHP,&nbsp...


How to Modify a Value in an Associative Array in PHP

In this lesson, we will see how to modify a value in an associative array in PHP, an Associative arr...


How to Add an Item to an Associative Array in PHP

In this lesson, we will see how to add an item to an associative array in PHP, an Associative array...


How to Add an Element to an Array in PHP

In this lesson, we will see how to add an element to an array in PHP, PHP has a built-in function th...