繁体   English   中英

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中,以便下次用户返回该地址时将在输入字段中回显。

我的代码有问题吗?

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"); }

您只为“ domain.tld”设置cookie

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

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/>";
      }
  }
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM