Nikk
Nikk

Reputation: 7921

php Cookie not working, won't get set

When I submit the form to the action processing script, I am supposed to set the cookie. But the cookie doesn't get set.

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

This is found on the processing script page.

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

What I am attempting is to save the email address in a cookie, so that the next time the user returns the address will be echoed in the input field.

Is there a problem with my code?

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

Upvotes: 0

Views: 558

Answers (2)

ghopst
ghopst

Reputation: 166

Following is the simplest snippet that does the magic

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

Upvotes: 0

Javad Khodadadi
Javad Khodadadi

Reputation: 408

you set cookie just for "domain.tld"

please test this code for all page of this address:

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

Upvotes: 1

Related Questions