1

I'am trying to set a cookie in a php file (yesterday it worked, today it does not)

I made the cookie and now i'm trying to delete is by setting it to a null-value. Still the cookie is not being made.

I already deleted the cookies in my browser aswell.

Can someone help me?

 setcookie("winkelwagen", "", '/');
 if(!isset($_COOKIE["winkelwagen"])) {
 echo "<br>Cookie is not set!";
 } else {
 echo "<br>Cookie '" . $_COOKIE["winkelwagen"] . "' is set!";
 echo "<br>Value is: " . $_COOKIE["winkelwagen"];
 }

I'm getting "Cookie is not set" as output

asked Apr 6, 2015 at 7:22
2
  • FYI: You cannot access cookie on the same request after setting it. Cookies are sent by browser as a part of HTTP Request Headers. And Cookies are sent by server as a part of HTTP Response Headers. So your HTTP request will not have the cookie which you are just setting in the response. Commented Apr 6, 2015 at 7:37
  • After a page reload it also says 'cookie not set' Commented Apr 6, 2015 at 7:45

2 Answers 2

2

This is supposed to fail / the expected behaviour:

setcookie("winkelwagen", "", '/');
if(!isset($_COOKIE["winkelwagen"])) {
 echo "<br>Cookie is not set!";
}

According to the manual (emphasis added by me):

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

So your cookie will never be available when you set it, it will be available on the next page you load.

answered Apr 6, 2015 at 7:30
Sign up to request clarification or add additional context in comments.

14 Comments

Yesterday I did try this: setcookie("winkelwagen", ""); and the output (echo's) were fine. it said cookie ... is set.
@user3475797 It will be if you have loaded the page before. Probably a previous cookie expired somewhere today / yesterday.
The thing is, even if i make a cookie with another name, that cookie is also not being created
@user3475797 Also check the manual for the exact order / meaning of the parameters.
@sergio Yep, it's a combination of both our answers :-)
|
1

You've made a mistake, you didn't setlife time (it is third parameter), but you set there path '/', which must be the fourth parameter. Also cookie doesn't set with empty value.

From the docs:

If the value argument is an empty string, or FALSE, and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client.

Use this:

 setcookie("winkelwagen", "some_value",time() + (86400 * 30), '/') //for eaxmle 86400 = 1 day
answered Apr 6, 2015 at 7:28

5 Comments

When i do: setcookie("winkelwagen", ""); it also does not work. So just name and value
try as in my example, it will set after reload the page.
Still no change. Cookie is not set
Do you reload the page? this works setcookie("winkelwagen11", "some_value",time() + (86400 * 30), '/'); As in my example. I checked.
Yes i reloaded the page

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.