The Developer Fastlane

« 365 days to become a developer » challenge

PHP: Cookies

November 9, 2020

Exercise: Instructions & result

Instructions

A form that ask the user for his/her birth year. If he/she is 18 years old or more, then the user will be allowed to access the webpage. Otherwise, an error will be displayed.

Result

Result

Click on one of the images above to access the exercise's website

For this exercise, we use:
  • setcookie() function (PHP Doc)
  • $_COOKIE super variable (PHP Doc)
    • unset() function can be used to unset the $_COOKIE super variable inside the current script
  • To clear the browser from the cookie, we need to use again the setcookie() function but with a time in the past: setcookie('user', 'value', time() - 10);
    • We do this because no clearcookie() function exists unfortunatly :)
    • Details about the time() function here: PHP Doc

Code

cookies.php

Code
<?php 

$age = null;
if ($_GET['action'] === "exit") {
  unset($_COOKIE['birthday']); 
      // Will unset the cookie for the current script but not for the session
  setcookie('birthday', '', time() - 10); 
      // Will unset the cookie for the session
}
if (!empty($_POST['birthday'])) { 
  setcookie('birthday', $_POST['birthday']); // 1. We set the cookie
  $_COOKIE['birthday'] = $_POST['birthday']; 
      // It's needed to call manually (if not, the server will ask for anonther input to call it)
}
if (!empty($_COOKIE['birthday'])) {
  $birthday = (int)$_COOKIE['birthday']; // 2. Then we call it do to smth.
  $age = (int)date('Y') - $birthday;
}

$title = 'Restricted content';
require 'includes/header.php';
?>

<p class="lead mb-5">This content is reserved to 18 years old and above users.</p>
</div>
<div class="row">
  <?php if ($age >= 18): ?>
    <div class="col-md-12">
      <div class="alert alert-success">You are allowed to watch this content.</div>
    </div>
    <div class="col-md-8">
      <h2>Lorem ipsum, dolor sit amet consectetur adipisicing elit.</h2>
      <p>Amet consectetur adipisicing elit. Eum accusantium officia esse eaque. Repellendus, vel facilis. Illum culpa perspiciatis quasi vel, voluptatem placeat earum nobis consequuntur repellat, mollitia, porro nisi.</p>
      <p>Consectetur adipisicing elit. Ipsa id earum, laborum exercitationem beatae a hic tempora aspernatur voluptatibus repellendus corrupti culpa natus tempore laudantium placeat ipsum optio. Reiciendis, possimus.</p>
    </div>
    <div class="col-md-4">
      <a href="cookies.php?action=exit">
        <div class="btn btn-danger float-right">Exit restricted area</div>
      </a>
    </div>
  <?php elseif ($age !== null): ?>
      <div class="col-md-12">
        <div class="alert alert-danger">You are to young to acces this content.</div>
      </div>
      <div class="col-md-4">
        <a href="cookies.php?action=exit"> // Setting a $_GET condition is the only way to unset a cookie completly
          <div class="btn btn-success">Change my age</div>
        </a>
      </div>
    </div>
  <?php else: ?>
    <div class="col-md-6 mb-5">
      <h2 class="pb-3">Verify your age</h2>
      <form action='cookies.php' method='POST'>
        <div class="form-group">
          <label for="birthday"><b>Select your birth year:</b></label>
          <select class="form-control" name="birthday">
            <?php for ( $year = date('Y') - 8; $year > date('Y') - 120; $year-- ): ?>
              <option value="<?= $year ?>" <?php if ($_POST['birthday'] == $year): ?>selected="true"<?php endif ?>>
                  <?= $year ?>
              </option>
            <?php endfor; ?>
          </select>
        </div>
        <button type="submit" class="btn btn-primary">Send</button>
      </form>
    </div>
    <div class="col-md-6">
      <p><b>Why this content is restricted?</b></p>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Explicabo cum provident at, aspernatur possimus perferendis a dignissimos mollitia repellendus dolor praesentium hic vero ab, quidem culpa sapiente voluptatum ipsam exercitationem.</p>
    </div>
  <?php endif; ?>
</div>

<?php require 'includes/footer.php'; ?>
© 2020 - Edouard Proust | The Developer Fastlane