linjuming
linjuming

Reputation: 2157

why can not change the cookie value using mootools?

why can not change the cookie value using mootools?
if I had set a cookie value in php I will fail to change the cookie value using mootools.
why faild? is it a bug of mootools?

<?php 
    setcookie('drres','hello');
?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js"></script>
<script type="text/javascript">

    function drres_cookie_read(){
        alert(Cookie.read('drres'));
    }
    function drres_cookie_write(){
        Cookie.write('drres','world');
        alert(Cookie.read('drres'));    // result is "hello" not "world",why?
    }
</script>
<button onclick="drres_cookie_read()">read</button>
<button onclick="drres_cookie_write()">write</button>

Upvotes: 0

Views: 199

Answers (2)

Arun Unnikrishnan
Arun Unnikrishnan

Reputation: 2349

You cannot set and access a cookie in the same instance/page.The browser identifies a cookie and stores it based on headers sent from the server to the browser.Technically you can't update a cookie, you can only overwrite it with a new one with the same name. You will have to do a redirect or refresh after setting it.Use the setcookie('drres','world'); to update the value.

Upvotes: 2

idmean
idmean

Reputation: 14905

I see. I think that's a cross scripting protection. You can't write or delete cookies set by the server. (Otherwise for example you will be able to override login cookies).

Upvotes: 1

Related Questions