php Cookie无法正常工作,无法设置

时间:2015-02-01 13:47:45

标签: php cookies

当我将表单提交给操作处理脚本时,我应该设置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"); }

2 个答案:

答案 0 :(得分:1)

您只为&#34; domain.tld&#34;

设置了Cookie

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

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

答案 1 :(得分:0)

以下是执行魔术的最简单的片段

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