Setcookie() inside AJAX request?

I have searched everywhere for this. I am sending info via an ajax request to a file “ajax.php” where I am processing that information. I want to put some of it into a ‘browser’ cookie.

Is this possible? I have seen several posts with this question, but people seem to just end up starting a session instead. I want to actually store the info into a browser cookie.

This is essentially my ajax.php that is called:

        $title = $_GET['title'];
        $word_list1 = $_GET['word_list1'];
	$word_list2 = $_GET['word_list2'];
	$cookie['title'] = $title;
	$cookie['list1'] = $word_list1;
        $cookie['list2'] = $word_list2;
	setcookie("word_sets", serialize($cookie));
echo ("Word sets saved");

Ajax functions correctly, and the ajax.php is executing correctly, but the cookies are not saved in the browser.

I figure it is possible by some means - any ideas?

That should work fine. How are you checking that the cookie is saved or not? It would not be readable by JavaScript in the page that made the AJAX request, not until the page is reloaded, as cookies are part of the HTTP request.

Actually nothing gets set - at all - after reloading the entire page. In addition to not being able to retrieve the cookie in the same or other pages, I am using a firefox extension that shows that no cookies are ever set.

If I use that exact code in a separate page that the form info is 'POST’ed to, the cookie is set fine, but using an ajax call to execute the code - they are not set.

I must be missing something, and I have been trying to find an answer online for hours - to no avail. I am sure the PHP code is executing in the ajax.php file, so it is weird.

Noob question - do I need to have some kind of headers explicitly set in the ajax.php for the browser to recognize that it need to accept the cookie (or something?)

Has anyone actually successfully completed setting a cookie via ajax?


You most likely need to specify the third argument to setcookie which dictates how long it will last.

setcookie("word_sets", serialize( $cookie ), time()+3600*24*1000)

Give that a shot, then do a print_r on $_COOKIE.

Alright, added the expires as indicated above, and changed my echo from “Words set saved” to:
echo (“<td>”.print_r($_COOKIE[‘word_sets’]).“</td>”);

Now the reply “looks” like the contents of the cookie (serialized), but it still is not set in the browser - even after reload. It doesn’t show up for retrieval in my firefox cookie viewer, or in code to read the cookie.

If printing $_COOKIE[‘word_sets’] prints anything, that’s proof positive the cookie was set. setcookie() does not modify $_COOKIE, which means what you printed was from a successfully set cookie on a previous page view.

Thing is, the code: $_COOKIE[‘word_sets’] - is just below setting the cookie in the ajax.php file = so when it is called, it is set then dispays the $_COOKIE value (that is in a variable?) - No refresh has been made. Printing $_COOKIE right after it is set (no refresh) shows the correct value - no cookie is set.

A print_r of $_COOKIE after a page refresh shows nothing - ie. there is not a cookie named “word_sets”

Thank you! You guys got me on the right path.

Turns out - DOES need an ‘expires’, but it also MUST HAVE a path. With a path in the setcookie - it works! Without a path - it doesn’t.