PHP:在AJAX请求开始时使用setcookie不保存cookie后请求?

whenever the ajax page is called, i run:

setcookie($filtersCookie, $cookieVal, time() + 86400); // 1 day

and when the page is refreshed, i use the following code to see if there were any past filters saved:

if(isset($_COOKIE[$filtersCookie])) {

but the cookie never exists after a page refresh. any ideas as to why this may happen?

setcookie is used before any browser output from the ajax call.

If your Ajax scripts resides in another directory than the calling page, then you should also use the path parameter

setcookie($filtersCookie, $cookieVal, time() + 86400, "/"); // 1 day

By default the cookie will be available from the directory the cookie is set on, using "/" will make it available to all paths.

I dont think the browser will intercept the cookie when ajax call. You can do a work around like setting the cookie from javascript.