简体   繁体   中英

Cookie value empty in ajax php script

when i make a jQuery AJAX request the cookie is being shown as empty. I tried setting a session instead and that works however i want this to be a cookie not a session. It is not cross domain and it is only storing a simple 1 character number. i want to retrieve the number, do something, then update the number. Sure i can use a session but i want this to be a cookie.

setcookie("currentResult", "", time()+60*60*24*30*12, "/", "*(mysite)**.com", 0,1);

then i add a value to it later in my script like this

$_COOKIE["currentResult"] = $ii;

Then when i call an AJAX php script like this;

 jQuery.ajax({
  type: "POST",
    url: "****(myscriptname.php)****",
     dataType: "html",
 data:  "start=" +start,
 success: function(data)
    {
      dataq = jQuery.trim(data);




    }  }); 

But alas the cookie is empty in that script. I am echoing it out on the page im on and its set fine and works no problem.

I tested doing the exact same with a session and the session is there in the AJAX script. I only seem to be having a problem with cookies.

It is any cookie i try to use in a ajax request.

I can only find other people talking about cross domain problems but this isnt cross domain... im confused! Please Help!

Take a look at the 7th parameter... 1 = don't let javascript see this cookie

httponly
When TRUE the cookie will be made accessible only through the HTTP protocol. This means that the cookie won't be accessible by scripting languages, such as JavaScript. It has been suggested that this setting can effectively help to reduce identity theft through XSS attacks (although it is not supported by all browsers), but that claim is often disputed. Added in PHP 5.2.0. TRUE or FALSE

The below was the solution! thanks Brad!!!

changed to; setcookie("currentResult", "", time()+60*60*24*30*12, "/", "*(mysite)**.com", 0,0); and its fixed!

Take a look at the 7th parameter... 1 = don't let javascript see this cookie

httponly When TRUE the cookie will be made accessible only through the HTTP protocol. This means that the cookie won't be accessible by scripting languages, such as JavaScript. It has been suggested that this setting can effectively help to reduce identity theft through XSS attacks (although it is not supported by all browsers), but that claim is often disputed. Added in PHP 5.2.0. TRUE or FALSE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM