Track Visits To A Page With PHP Sessions


This script maintains two session variables -- a count of the number of times you have viewed the page and the timestamp of the last visit.

Each time the page is loaded, the old values are displayed and the new values set. If you surf to other websites and then come back, these values are remembered, but if close your web browser, and come back, the values are reset.


Script Source



if ($_SESSION["last_visit"]) {
echo "Date of last visit: ";
echo date("j F Y, H:i:s", $_SESSION["last_visit"]);
echo "<br>";
echo "Total visits: ".$_SESSION["num_visits"];
echo "This is your first visit";

$_SESSION["last_visit"] = time();


Please let me know if this script was helpful to you.