Source Code for /public/appendix-samples/welcome-back-counter.php
<?php
function set_or_update_visit_counter_cookie( $value ) {
setcookie( 'visited', $value, time() + 60 * 60 * 24 * 7 );
}
if( isset( $_COOKIE['visited'] ) ) {
$number_of_visits = intval( $_COOKIE['visited'] );
echo "👋 Welcome back! You've visited this page ";
echo $number_of_visits;
echo " time(s) before!";
$number_of_visits++;
set_or_update_visit_counter_cookie( $number_of_visits );
} else {
echo "🎉 Welcome! This is your first visit.";
set_or_update_visit_counter_cookie( 1 );
}