Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP – setcookie() not working

Tags:

php

cookies

I have this page that sets a cookie and echos out a string if you check a checkbox. The string prints correctly, but the cookie never gets set and I have no idea why.

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<label for="checkbox">Option 1:</label>
<input type="checkbox" name="checkbox" id="checkbox"><br>
<input type="submit" name="submit" value="Submit">
</form>
  <?php
if (isset($_POST['checkbox'])) {
  setcookie("cookie", "on", time()+3600*24);
  echo "You checked the checkbox and a cookie was set with a value of:<br>";
}
else {
  setcookie("cookie", "off", time()+3600*24);
  echo "You didn't check the checkbox and a cookie was set with a value of:<br>";
}
echo $_COOKIE['cookie'];
  ?>

Does anyone know why the above code does not work?

like image 647
Tim Avatar asked Jul 09 '14 19:07

Tim


People also ask

What is Setcookie () function?

The setcookie() function defines a cookie to be sent along with the rest of the HTTP headers. A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too.

At what point in a PHP file should the Setcookie () function be called?

PHP provided setcookie() function to set a cookie. This function requires upto six arguments and should be called before <html> tag.

How do I enable cookies in PHP?

You can do this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive on in your php. ini or server configuration files. Common Pitfalls: Cookies will not become visible until the next loading of a page that the cookie should be visible for.

How do I set the HttpOnly flag on cookies in PHP?

Set HttpOnly cookie in PHPini_set("session. cookie_httponly", True); This is the most common way to set cookies in PHP, empty variables will hold their default value.


2 Answers

PHP superglobals are populated at script start-up time, and then are NOT modified or touched by PHP again for the life of the script. That means $_COOKIE represents the cookies that were sent to the server in the http request that fired up the script. It will NOT show any cookies you've added/changed/deleted during the life of the script. Those changes will only show up on the NEXT request.

The only exception to this is $_SESSION, which is populated when you call session_start().

If you need those values to be in $_COOKIE immediately, you'll have to add them manually, e.g.

setcookie('cookie', $value, ....);
$_COOKIE['cookie'] = $value;
like image 196
Marc B Avatar answered Oct 02 '22 15:10

Marc B


According to the PHP Manual at http://php.net/manual/en/function.setcookie.php:

If output exists prior to calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie.

In other words, the function setcookie() is not working because it is inside the page. If you want it to work, you will need to put that function before the page, specifically before any headers.

Do this:

<?php
  if ( isset($_POST['checkbox']) ) {
     setcookie("cookie", "on", time()+3600*24);
     echo "You checked the checkbox and a cookie was set with a value of:<br>";
  } else {
     setcookie("cookie", "off", time()+3600*24);
     echo "You didn't check the checkbox and a cookie was set with a value of:<br>";
  }

  echo $_COOKIE['cookie'];
?>
<!doctype html>
<html>
  <head>...</head>
  <body>
      <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
         <label for="checkbox">Option 1:</label>
         <input type="checkbox" name="checkbox" id="checkbox"><br>
         <input type="submit" name="submit" value="Submit">
      </form>
  </body>
</html>
like image 20
JDot Avatar answered Oct 02 '22 16:10

JDot