welcome.php

<?php
/* Cookie demo: user visits for the first time, and is asked to
   "register" (just provide a name).  A cookie is set containing this
   information, and the user then is allowed access to the site
   content.  There is also a mechanism for user to delete the cookie.

   Script is in 3 sections:

   1. Preliminaries where the existence of a cookie or a request is
   determined.  This needs to be done early, before any output, so the
   cookie can be set if need be.

   2. Welcome section, where the visitor gets to see the site content
   based on the cookie being set.  This includes a button for
   resigning (unregistering), which clears the cookie.

   3. First-visit section, before cookie is set, where visitor sees a
   form to register.
 */

/* Section 1: preliminaries */

  $cookie_name = 'whatsyourname';   // name of cookie holding visitor name
  $name_field_name = 'visitorname'; // name of form field where visitor enters name
  $unregister_field_name = 'unregister'; // name of form field for unregistering
  $timeout = 3600; // time for cookie to expire, in seconds: 1 hour

// The setcookie call needs to precede any output, so we check here and
// set it if we have a value from the form, but remember for later output.
// Note that when receiving the response, cookie has not yet been set
// (it is set when the page is sent), but we treat it as if cookie was set.
  $cookie_set = isset($_COOKIE[$cookie_name]);
  $register = isset($_POST[$name_field_name]);
  $unregister = isset($_POST[$unregister_field_name]);
  if( $cookie_set ) {
    $visitor_name = $_COOKIE[$cookie_name];
// set the cookie again so that expiration timer is reset by visit activity
    setcookie($cookie_name,$visitor_name,time()+$timeout);

// if registered visitor clicked the unregister button, delete the cookie
    if( $unregister ) {
      setcookie($cookie_name,FALSE);
      $cookie_set = FALSE;  // act as if cookie not set anymore, though that will not happen till page loads
    }
  }
  else if( $register ) { // cookie not yet set, but request will set it
    $visitor_name = $_POST[$name_field_name];
    if( strlen($visitor_name) ) { // ignore if empty
      setcookie($cookie_name,$visitor_name,time()+$timeout);
      $cookie_set = TRUE;
    }
  }
?>

<!DOCTYPE html>
<html lang="en">
  <head>
<?php
  if( $cookie_set ) {
?>
    <title>Welcome!</title>
<?php } else { ?>
    <title>Register</title>
<?php } ?>
    <meta charset="utf-8">
  </head>
  <body>
    <pre>
<?php
// Print the cookies and post data as debugging aid.
// Escape special characters: print_r(x,true) means return a string instead of printing
 print '$_COOKIE: ' . htmlspecialchars(print_r($_COOKIE,true));
 print '$_POST: ' . htmlspecialchars(print_r($_POST,true));
?>
    </pre>
    
<?php
/* Section 2: Welcome */

  if( $cookie_set ) {
?>
  <h1>
<?php
  if( $register ){ print "Welcome, "; }  // save "Welcome back" for later visits
  else { print "Welcome back, "; }
  print htmlspecialchars($visitor_name);
?>
  </h1>
  <p>Enjoy all the amenities of our site.
  Take a look at <a href="welcome2.php">page 2</a>.
  </p>
  <form method="post" action="<?php print $_SERVER['SCRIPT_NAME'] ?>">
    <p><input type="hidden" name="unregister" value="unregister" />
       <input type="Submit" value="Unregister" /> Click here to resign from club.</p>
  </form>
<?php } else {
/* Section 3: Register */
?>
    <h1>Please register</h1>
  <form method="post" action="<?php print $_SERVER['SCRIPT_NAME'] ?>">
  <p>Enter your name: <input name="<?php print $name_field_name; ?>" type="text" />
  </p>
  <p><input type="Submit" value="Register" />
  </p>
  </form>
<?php } ?>
  </body>
</html>