Using Sessions on a Single Page in PHP
In the first example on sessions, we wrote something to the session in one file and read it in another. Actually, this is not necessary - it can be done in a single file.
For example, you can make a page refresh counter for the site user. To do this, on the user's first visit, we will write one to the session variable, and on all subsequent visits, we will increment this variable by one:
<?php
session_start();
if (!isset($_SESSION['counter'])) {
$_SESSION['counter'] = 1; // first visit to the page
} else {
$_SESSION['counter']++; // subsequent visits
}
echo $_SESSION['counter'];
?>
Save the user's visit time to the session. When the page is refreshed, display how many seconds ago the user visited the site.