PHP session is easily started by making session_start () this task starts to check if the session has started and if no startups start one. It is recommended to place a call in session_start () at the beginning of the page.

The session dynamics are stored in the same assembly members called $ _SESSION []. These variables can be achieved during the life of the session.

The following example starts a session and registers a variable called a counter that expands each time a page is visited during a session.

Use the isset () function to check whether a session variable has already been set or not.

Enter this code in a example.php file and upload this file over and over to see the result -


#example.php

<?php
session_start();

if (isset($_SESSION['counter'])) {
    $_SESSION['counter'] += 1;
} else {
    $_SESSION['counter'] = 1;
}

$msg = "You have visited this page " . $_SESSION['counter'];
$msg .= "in this session.";
echo $msg;
?>


Output:

You have visited this page 1in this session.

Destroying a PHP Session

PHP session(time) can be destroyed by session_destroy () function. This operation does not require an argument and a single call can disrupt all session dynamics. If you want to break one session variable you can use the unset () function to set the session variable.

Here is an example of setting one variable -

<?php
   unset($_SESSION['counter']);
?>