<!DOCTYPE html>
<html lang="en">

<head>
  <?php require_once "../../base/meta.php" ?>

  <title>Counter</title>
</head>

<body>
  <?php
  // PHP
  require_once "../../base/settings.php";
  require_once "../../base/headers.php";
  Headers::html();

  $fh = fopen("./data/counter.txt", "r+");
  if (flock($fh, LOCK_EX)) {
    $cnt = intval(fgets($fh));

    if (!isset($_COOKIE["counter-visit"])) {
      rewind($fh);
      $cnt += 1;
      fputs($fh, strval($cnt));

      setcookie("counter-visit", "true",  ["expires" => time() + 60 * 60 * 24, "samesite" => "strict", "secure" => true, "httponly" => true]);
    }
  }
  flock($fh, LOCK_UN);
  fclose($fh);
  ?>
  <h1>Visitor count: <?= $cnt ?></h1>
</body>

</html>