How to create a cookie to store the timestamp of when a page is first loaded with php

I would really appreciate any help if possible!

This is the question:
Use a cookie (NOT PHP SESSION) that expires a year from now to do the following:
Record the timestamp of the FIRST load of this page (but NOT any subsequent load of this page).

The code:

<!DOCTYPE html>
    <title>User Profile</title>

     User Profile:
     <form action="" method="POST" name="form1" onsubmit="return validate_form()">
      <input type="hidden" name="task" value="process_profile">

       <input type="checkbox" name="is_cool" value="yes">
       Are you cool?
       What Bands do you like?
       <select name="like_bands[]" multiple>  <small>* Required Field</small>
          <option value="Sabbath">Black Sabbath</option>
          <option value="Mastodon">Mastodon</option>
          <option value="Metallica">Metallica</option>
         <option value="Swift">Taylor Swift</option>
       Favorite band not in the above list.
       <input type="text" name="other_band" value="">
       <button type="submit"> Continue/Confirm </button>

      // Client-side form validation
      // Don't change the names of stuff in the form, and you won't have to change anything below
      // Used built-in JS instead of JQuery or other validation tool
      function validate_form() {

        var form_obj = document.form1;

        var count_liked = 0;
        for (var i=0 ; i < form_obj['like_bands[]'].options.length ; i++ ) {
          if (form_obj['like_bands[]'].options[i].selected) {

        if (count_liked == 0) {
          alert("You must choose a band from the menu.");
          return false; // cancel form submission

        return true;  // trigger form submission


>Solution :

Check if the cookie is already set. If not, set the cookie to the current timestamp.

if (!isset($_COOKIE['load_date'])) {
    setcookie('load_date', time(), time() + 86400*365);

Leave a Reply