php Cookie 无效,无法设置

php Cookie not working, won't get set

当我将表单提交给动作处理脚本时,我应该设置 cookie。但是 cookie 没有设置。

<input type="email" name="fes-email" class="fes-input" value="<?php echo $_COOKIE['hotspot-user-email']; ?>" placeholder="Еmail">

这是在处理脚本页面上找到的。

setcookie('hotspot-user-email', $_POST['fes-email'], time() + (86400 * 30), 'domain.tld'); 

我正在尝试的是将电子邮件地址保存在 cookie 中,以便下次用户 returns 该地址将在输入字段中回显。

我的代码有问题吗?

if(!empty($_POST['fes-email'])) {

            if (filter_var($_POST['fes-email'], FILTER_VALIDATE_EMAIL)) {

                setcookie('hotspot-user-email', $_POST['fes-email'], time() + (86400 * 30), '/'); // 86400 = 1 day

            }else { echo("EMAIL IS NOT VALID"); }

        }else { echo("EMPTY FIELD"); }

你设置 cookie 只是为了 "domain.tld"

请为此地址的所有页面测试此代码:

setcookie('hotspot-user-email', $_POST['fes-email'], time() + (86400 * 30), '/'); 

以下是最简单的代码片段,可以发挥神奇作用

<?php
  if (isset($_COOKIE['testCookie'])) {
      echo "<br/> Cookie is set: " . $_COOKIE['testCookie'] ." <br/>";
  } else {
      if (setcookie('testCookie','myemail@mydomain.com', time() + 86400)) {
          echo "<br/> cookie is set <br/>";
      }
  }
?>