Source Code for /public/06-retaining-state/welcome-back.php

<?php
// See if a cookie is set:
if( isset( $_COOKIE['visited'] ) ) {
  // The cookie is set, so we welcome the user back.
  echo "👋 Welcome back! You've visited this page before!";

  // We could optionally set the cookie again here to renew it/
  // extend its lifespan.

} else {
  // Cookie is not set: welcome them!
  echo "🎉 Welcome! This is your first visit.";

  // We need to specify the number of SECONDS the cookie will
  // last for, so we need to do some arithmetic:
  $cookie_lifespan = 60  /* seconds in a minute */
                   * 60  /* minutes in an hour */
                   * 24  /* hours in a day */
                   *  7; /* days (a week) */

  // Now we can set the cookie, which will last for a week.
  // Our cookie just sets visited=1 and will be accessible
  // on subsequent requests via $_COOKIE['visited'].
  setcookie( 'visited', '1', time() + $cookie_lifespan );
}