⊗ppPmCkPRC 327 of 447 menu

Page Refresh Counter Using Cookies in PHP

Let's create a page refresh counter:

<?php if (!isset($_COOKIE['counter'])) { // first visit to the page setcookie('counter', 1); $_COOKIE['counter'] = 1; } else { setcookie('counter', $_COOKIE['counter'] + 1); $_COOKIE['counter'] = $_COOKIE['counter'] + 1; } echo $_COOKIE['counter']; ?>

The code can be simplified using the ++ operator:

<?php if (!isset($_COOKIE['counter'])) { // first visit to the page setcookie('counter', 1); $_COOKIE['counter'] = 1; } else { setcookie('counter', ++$_COOKIE['counter']); } echo $_COOKIE['counter']; ?>

Record the timestamp of the user's first visit to the page in a cookie. Upon page refresh, display how much time has passed since the moment of the first visit to the page.

byenru