如何创建一个 cookie 来存储页面首次加载时的时间戳 php

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

如果可能的话,我将不胜感激!

这是问题: 使用从现在起一年后过期的 cookie(不是 PHP SESSION)来执行以下操作: 记录此页面首次加载的时间戳(但不记录此页面的任何后续加载)。

代码:

<!DOCTYPE html>
<html>
  <head>
    <title>User Profile</title>
  </head>
  <body>
     <h3><?=$message?></h3>

     User Profile:
     <br><br>
     <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?
       <br><br>
       What Bands do you like?
       <br>
       <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>
       </select>
       <br><br>
       Favorite band not in the above list.
       <br>
       <input type="text" name="other_band" value="">
       <br><br>
       <button type="submit"> Continue/Confirm </button>
     </form>

     <script>
      //////////////////////////////////////////////////////////////////////////////////////////////////////////
      // 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) {
            count_liked++;
          }
        }

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

        return true;  // trigger form submission
      }
    </script>

  </body>
</html>

检查cookie是否已经设置。如果不是,则将 cookie 设置为当前时间戳。

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