<small id='8THEi'></small><noframes id='8THEi'>

    1. <legend id='8THEi'><style id='8THEi'><dir id='8THEi'><q id='8THEi'></q></dir></style></legend>

      1. <tfoot id='8THEi'></tfoot>

          <bdo id='8THEi'></bdo><ul id='8THEi'></ul>

        <i id='8THEi'><tr id='8THEi'><dt id='8THEi'><q id='8THEi'><span id='8THEi'><b id='8THEi'><form id='8THEi'><ins id='8THEi'></ins><ul id='8THEi'></ul><sub id='8THEi'></sub></form><legend id='8THEi'></legend><bdo id='8THEi'><pre id='8THEi'><center id='8THEi'></center></pre></bdo></b><th id='8THEi'></th></span></q></dt></tr></i><div id='8THEi'><tfoot id='8THEi'></tfoot><dl id='8THEi'><fieldset id='8THEi'></fieldset></dl></div>
      2. php setcookie 不适用于 ajax 调用

        时间:2024-08-10

          <tfoot id='mueS6'></tfoot>

                  <bdo id='mueS6'></bdo><ul id='mueS6'></ul>

                    <tbody id='mueS6'></tbody>

                  <small id='mueS6'></small><noframes id='mueS6'>

                • <legend id='mueS6'><style id='mueS6'><dir id='mueS6'><q id='mueS6'></q></dir></style></legend>
                • <i id='mueS6'><tr id='mueS6'><dt id='mueS6'><q id='mueS6'><span id='mueS6'><b id='mueS6'><form id='mueS6'><ins id='mueS6'></ins><ul id='mueS6'></ul><sub id='mueS6'></sub></form><legend id='mueS6'></legend><bdo id='mueS6'><pre id='mueS6'><center id='mueS6'></center></pre></bdo></b><th id='mueS6'></th></span></q></dt></tr></i><div id='mueS6'><tfoot id='mueS6'></tfoot><dl id='mueS6'><fieldset id='mueS6'></fieldset></dl></div>
                • 本文介绍了php setcookie 不适用于 ajax 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个页面 test.php,代码如下:

                  I have a page, test.php, with the following code:

                  <html>
                      <body>
                          <form>
                              <script type="text/javascript"> 
                  
                                  function SendCookies(){
                  
                                      if (window.XMLHttpRequest)/* code for IE7+, Firefox, Chrome, Opera, Safari */
                                      { xmlhttp=new XMLHttpRequest(); }
                                      else /* code for IE6, IE5 */
                                      { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
                  
                                      xmlhttp.onreadystatechange=function()
                                      {
                                          if (xmlhttp.readyState==4 && xmlhttp.status == 200)
                                          {
                                              alert('done');
                                          }
                                      }
                  
                                      xmlhttp.open("GET", "/web/DEV/Classes/SetCookie.php?time=" + new Date());
                                      xmlhttp.send();
                  
                                  }
                  
                              </script>
                  
                              <input type="text" id="txtInput" name="txtInput"/>
                              <input type="button" id="btnSubmit" name="btnSubmit" value="Submit"  onclick="SendCookies()"/>
                              <div id="divTest">
                                  <?php
                                      if (isset($_COOKIE["TestCookie"])) {
                                          echo $_COOKIE["TestCookie"];
                                      } else {
                                          echo "__Results__";
                                      }
                                  ?>          
                              </div>
                          </form>
                      </body>
                  </html>
                  

                  我有一个页面,SetCookie.php,代码如下:

                  I have a page, SetCookie.php, with the following code:

                  <?php 
                      $var = "THIS IS  A  TEST";
                      setcookie("TestCookie", $var, time()+60*60*24*30);
                  ?>
                  

                  当点击 test.php 的按钮时,我使用 XMLHttpRequest 来调用我的 SetCookie.php 页面.页面执行,因为如果我向它添加回声,我会在 xmlhttp 响应中得到它.但是,似乎没有设置 TestCookie.

                  When test.php's button is clicked, i use XMLHttpRequest to call my SetCookie.php page. The page executes, becuase if i add an echo to it, i get that in the xmlhttp response. However, TestCookie does not seem to be getting set.

                  如果在 text.php 中,我执行在 SetCookie.php 中找到的相同命令,然后为所有浏览器会话设置相应的 cookie.

                  If in text.php, i do the same command found in SetCookie.php, the cookie is then set accordingly for all browser sessions.

                  即使在我关闭/打开浏览器之后,cookie 与我曾经在我的 test.php 页面中手动设置时一样保持不变.

                  Even after i close / open the browser, the cookie remains unchanged from when i once set it in my test.php page manually.

                  ----编辑-----

                  我补充说:

                  if(!setcookie("TestCookie", "A", time()+60*60*24*30, "/")) {
                      echo "FAIL";
                  }
                  

                  到 test.php 的最顶部,但是当我重新加载页面时,它永远不会显示更新的 cookie... 因为该 cookie 已经在没有/"参数的情况下设置,并且以后无法修改, 带有 ,"/" 参数.

                  to the very top of test.php, however when i reload the page, it never shows the updated cookie... because that cookie was already set without the ,"/" parameter, and cannot be modified later, with the ,"/" parameter.

                  在清除缓存并使用建议的代码后,我从浏览器中清除了我的 cookie,并为 set 方法使用了添加的参数,我能够从所有页面操作 cookie!非常感谢!!

                  After clearing the cache and working with the suggested code, i cleared my cookies from the browser and used the added parameter for the set method, i was able to manipulate the cookies from all pages!!! thank you so much!!

                  推荐答案

                  如果不给 setcookie() 添加 $path 值,则默认为 "当前目录".这意味着如果您从 /web/DEV/Classes/SetCookie.php 设置 cookie,则 cookie 将设置为 /web/DEV/Classes/ 以及以上任何内容该路径不会看到该 cookie.

                  If you don't add a $path value to setcookie(), it defaults to "the current directory". This means that if you set the cookie from /web/DEV/Classes/SetCookie.php, the cookie gets set to /web/DEV/Classes/, and anything above that path won't see that cookie.

                  要解决此问题,请将特定的 $path 添加到 setcookie.如果您的应用在域根目录 (example.com) 上运行,请使用 '/'.如果它位于子文件夹 (example.com/myapp/) 中,请使用 '/myapp/'

                  To fix this, add a specific $path to setcookie. If your app runs on the domain root (example.com), use '/'. If it's in a subfolder (example.com/myapp/), use '/myapp/'

                  setcookie("TestCookie", $var, time()+60*60*24*30, '/');
                  

                  这篇关于php setcookie 不适用于 ajax 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:XMLHttpRequest &amp;FormData 未提交数据 下一篇:删除先前的标记并在更新的 lat lng 中添加标记

                  相关文章

                  <legend id='xcN68'><style id='xcN68'><dir id='xcN68'><q id='xcN68'></q></dir></style></legend>
                  • <bdo id='xcN68'></bdo><ul id='xcN68'></ul>
                  1. <i id='xcN68'><tr id='xcN68'><dt id='xcN68'><q id='xcN68'><span id='xcN68'><b id='xcN68'><form id='xcN68'><ins id='xcN68'></ins><ul id='xcN68'></ul><sub id='xcN68'></sub></form><legend id='xcN68'></legend><bdo id='xcN68'><pre id='xcN68'><center id='xcN68'></center></pre></bdo></b><th id='xcN68'></th></span></q></dt></tr></i><div id='xcN68'><tfoot id='xcN68'></tfoot><dl id='xcN68'><fieldset id='xcN68'></fieldset></dl></div>
                  2. <small id='xcN68'></small><noframes id='xcN68'>

                    <tfoot id='xcN68'></tfoot>