Note that there are some explanatory texts on larger screens.

plurals
  1. POphp setcookie not working with ajax call
    primarykey
    data
    text
    <p>I have a page, test.php, with the following code:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;form&gt; &lt;script type="text/javascript"&gt; 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 &amp;&amp; xmlhttp.status == 200) { alert('done'); } } xmlhttp.open("GET", "/web/DEV/Classes/SetCookie.php?time=" + new Date()); xmlhttp.send(); } &lt;/script&gt; &lt;input type="text" id="txtInput" name="txtInput"/&gt; &lt;input type="button" id="btnSubmit" name="btnSubmit" value="Submit" onclick="SendCookies()"/&gt; &lt;div id="divTest"&gt; &lt;?php if (isset($_COOKIE["TestCookie"])) { echo $_COOKIE["TestCookie"]; } else { echo "__Results__"; } ?&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I have a page, SetCookie.php, with the following code:</p> <pre><code>&lt;?php $var = "THIS IS A TEST"; setcookie("TestCookie", $var, time()+60*60*24*30); ?&gt; </code></pre> <p>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.</p> <p>If in text.php, i do the same command found in SetCookie.php, the cookie is then set accordingly for all browser sessions.</p> <p>Even after i close / open the browser, the cookie remains unchanged from when i once set it in my test.php page manually.</p> <p><strong>----EDIT-----</strong></p> <p>I added:</p> <pre><code>if(!setcookie("TestCookie", "A", time()+60*60*24*30, "/")) { echo "FAIL"; } </code></pre> <p>to the very top of test.php, however when i reload the page, it never shows the updated cookie... <strong>because that cookie was already set without the ,"/" parameter, and cannot be modified later, with the ,"/" parameter.</strong></p> <p>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!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload