PHP Tutorial


PHP 7 Session


PHP Session

When you work with an application, you open it, make some changes and then you close it. It is like a session.

The PHP session is a variable that stores user information to be used across multiple pages. By default, the session variable runs until the user closes the browser.

The PHP session acts as an identifier for the webserver. In which this session identifier or SID is a unique number that is used to identify each user in the session.


Start a PHP Session: session_start()

The session_start() function in PHP is a built-in function that is used to start a session. This function starts a new session or resumes an already existing session and returns the current session. If no session is available, it creates and returns a new session ID for the user.

Syntax:

    session_start();

Note: PHP session_start() function does not require any parameter to start a session.


Example:

Create a Program to start a session with PHP session_start() function. 

    <?php

        session_start();    // For start a session.

    ?>


Store The Session Data: $_SESSION[]

The PHP superglobal variable $_session[] stores the data of the session in an associative array. It is used to set and receive all the variables values present in the session.

Before setting the session variable, it is necessary to check whether the session variable is set or not, for which the isset () function is used.

Syntax:

    $_SESSION[session_name] = value;


Example:

Create a program to set the session variable and get the variable value with PHP $_SESSION[] Superglobal variable. 

    <?php

        session_start();

        $_SESSION["Username"] = "David";        // Set a session variable.

        /* Check the session is set or not and get the session variable value. */

        if (isset($_SESSION["Username"])) {

            echo "The session variable is set and the variable value is:" . "<br>";

            print_r( $_SESSION ); // Use print_r() function to print the value of session variable.
            
        } else {

            echo "The session is not set.";

        }
        

    ?>


Remove PHP Session Variable

In PHP, The session_unset() is used to remove all session variables, but unset() function is used to remove a certain session variable.

Syntax:

    session_unset();    

    or 

    unset(session_variable);    

Note: PHP session_unset() function does not require any parameter to remove all session variables.


Example:

Create a program to remove all session variables with PHP session_unset() function.

    <?php

        session_start();

        session_unset();    // For remove all session variables.

        print_r( $_SESSION );

    ?>

Create a program to remove a certain session variable with PHP unset() function.

    <?php

        session_start();

        $_SESSION["Username1"] = "John";

        $_SESSION["Username2"] = "David";

        unset($_SESSION['Username2']);    // For unset a session variable.

        if (!isset($_SESSION["Username2"])) {

            echo "the session variable is removed.";

        } else {

            echo "the session variable is not removed.";
        }

    ?>

Destroy PHP Session: session_destroy()

In PHP, The session_destroy() function is used to completely destroy a session
 
Syntax:

    session_destroy();        // For completely destroy a session.

Note: Note: PHP session_destroy() function does not require any parameter to destroy completely a session.


Example:

Create a program to completely destroy a session with PHP session_destroy() function.

    <?php

        session_start();

        session_unset();    // For remove all session variables.

        print_r( $_SESSION );

    ?>


Example:

Create a program to automatic expire session in 15 minutes.

    <?php

        session_start();

        $current_time = 30;
        
        if(isset($_SESSION['last_activity'])){
            
            $seconds_inactive = time() - $_SESSION['last_activity'];
            
            $expire_after = $current_time * 30;
            
            if($seconds_inactive >= $expire_after){
                
                session_unset();
                session_destroy();
            }
            
        }else{

            $_SESSION['last_activity'] = time();

        }

    ?>

Example:

Create a program for a session set with a PHP associative array.

    <?php

        session_start();

        $array = ["session_name1" => "value1", "session_name2" => "value2", "session_name3" => "value3"];

        print_r($array);

        $_SESSION["user"] = $array;    

        /* Check the session is set or not and get the session variable value. */

        if (isset($_SESSION)) {

            echo "The session variable is set and the variable value is:" . "<br>";

            print_r( $_SESSION ) ;
            
        } else {

            echo "The session is not set.";

        }
        
    ?>

Example: 

Create a program for a session set with the counter.

    <?php  

        session_start();

        if (!isset($_SESSION['counter'])) { 

            $_SESSION['counter'] = 1;  

           } else {  

            $_SESSION['counter']++;  

           }  

           echo (" How many times reload Page: ".$_SESSION['counter']); 
        

    ?>