SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Colorado
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    Code:
            $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?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Colorado
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

    TIA

  4. #4
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You most likely need to specify the third argument to setcookie which dictates how long it will last.

    Code:
    setcookie("word_sets", serialize( $cookie ), time()+3600*24*1000)
    Give that a shot, then do a print_r on $_COOKIE.
    Cross browser css bugs

    Dan Schulz you will be missed

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Colorado
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Colorado
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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"

  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Colorado
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •