简体   繁体   English

Cookie值在Ajax php脚本中为空

[英]Cookie value empty in ajax php script

when i make a jQuery AJAX request the cookie is being shown as empty. 当我发出jQuery AJAX请求时,cookie被显示为空。 I tried setting a session instead and that works however i want this to be a cookie not a session. 我尝试设置一个会话来代替,并且可以,但是我希望它是一个cookie,而不是一个会话。 It is not cross domain and it is only storing a simple 1 character number. 它不是跨域的,仅存储一个简单的1个字符。 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. 当然,我可以使用会话,但是我希望它是一个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; 然后当我像这样调用AJAX php脚本时;

 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. 但是可惜cookie在那个脚本中是空的。 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. 我测试了使用会话执行完全相同的操作,并且该会话在AJAX脚本中存在。 I only seem to be having a problem with cookies. 我似乎只对cookie有问题。

It is any cookie i try to use in a ajax request. 它是我尝试在ajax请求中使用的任何cookie。

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 看一下第7个参数... 1 =不要让javascript看到此cookie

httponly 仅Http
When TRUE the cookie will be made accessible only through the HTTP protocol. 设为TRUE时,只能通过HTTP协议访问cookie。 This means that the cookie won't be accessible by scripting languages, such as JavaScript. 这意味着无法通过脚本语言(例如JavaScript)访问Cookie。 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. 有人建议此设置可以有效地帮助通过XSS攻击减少身份盗用(尽管并非所有浏览器都支持此功能),但是这种说法经常引起争议。 Added in PHP 5.2.0. 在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); 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 看一下第7个参数... 1 =不要让javascript看到此cookie

httponly When TRUE the cookie will be made accessible only through the HTTP protocol. httponly如果为TRUE,则只能通过HTTP协议访问cookie。 This means that the cookie won't be accessible by scripting languages, such as JavaScript. 这意味着无法通过脚本语言(例如JavaScript)访问Cookie。 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. 有人建议此设置可以有效地帮助通过XSS攻击减少身份盗用(尽管并非所有浏览器都支持此功能),但是这种说法经常引起争议。 Added in PHP 5.2.0. 在PHP 5.2.0中添加。 TRUE or FALSE 对或错

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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