Cookie behaves different when URL ends with slash or not

806 views
Skip to first unread message

woomla

unread,
Mar 10, 2010, 7:21:57 AM3/10/10
to MooTools Users
If I have a cookie in a subdirectory, this cookie is read from even
when I'm in the root. When I write the cookie, the cookie's path is
the root. So reading and writing goes to different cookies (same name,
different path).

Here's how:

When I browse to a directory on my webserver it automatically loads
the index.html file located in that directory. Example: http://mywebserver/CookieTest,
no slash at the end.

If i set a cookie and read it, it is changed every time.
If you view the cookie information (with i.e. FireCookie), you see the
cookie (in the root) beeing changed.
This is good.

Now if I browse to: http://mywebserver/CookieTest/, the same
index.html file is loaded.
If I set a cookie and read it, it is changed every time.
If you view the cookie information (with i.e. FireCookie), you see the
cookie (in the /CookieTest/) beeing changed.
This is good.

Now if you go back to http://mywebserver/CookieTest, the same
index.html is loaded.
Now if you set the cookie, the cookie in the root (/) is set, but the
cookie in the directory (/CookieTest/) is read.
Now there's a difference between reading and writing the Cookie.
This is *not* good.

Maybe it's not good practice to use a url without /. Should I have my
webserver append it always? If so, then how?

I think the problem boils down to Cookie.read that does a match and
finds the 'wrong' cookie. I'll try to see if I can get more
information on this.

I don't know if this is a bug, but it did surprise me to.


I've tried to setup a mooshell example, but mooshell always puts the
slash at the end, so it doesn't work.
But you can find the test at: http://www.mootools.net/shell/3UQkR/2/.

Here's a sample index.html file. You have to put this in a directory
on your webserver i.e. CookieTest together with mootools.js.

============ index.html ================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="/CookieTest/mootools.js" type="text/javascript"></
script>

<meta http-equiv="content-type" content="text/html;
charset=windows-1250">
<meta name="generator" content="Woomla">
<title>Cookie Test</title>
<script type="text/javascript">

function info(text)
{
$('info').appendText(text);
$('info').grab(new Element('br'));
}

function getTheCookie()
{
var value = Cookie.read('cookietest.key');
info('get: ' + value);
}

function setTheCookie()
{
var value = new Date().getTime();
info('set: ' + value);
Cookie.write('cookietest.key', value, {'duration': 3600});
getTheCookie();
}


window.addEvent('domready', function ()
{
$('set').addEvent('click', setTheCookie);
getTheCookie();
});
</script>

<style type="text/css">
<!--
#info
{
border:1px dashed black;
width:90%;
padding:5px;
}
//-->
</style>
</head>
<body>

<button type="button" id="set">set</button>
<br />
<br />
<div id="info"></div>

</body>
</html>

rasmusfl0e

unread,
Mar 10, 2010, 8:06:01 AM3/10/10
to MooTools Users
The MooTools docs state:

* In order to share the Cookie with pages located in a different
path, the Cookie.options.domain value must be set.

Read The Friendly Manual :P

On Mar 10, 1:21 pm, woomla <woo...@gmail.com> wrote:
> If I have a cookie in a subdirectory, this cookie is read from even
> when I'm in the root. When I write the cookie, the cookie's path is
> the root. So reading and writing goes to different cookies (same name,
> different path).
>
> Here's how:
>
> When I browse to a directory on my webserver it automatically loads
> the index.html file located in that directory. Example:http://mywebserver/CookieTest,
> no slash at the end.
>
> If i set a cookie and read it, it is changed every time.
> If you view the cookie information (with i.e. FireCookie), you see the
> cookie (in the root) beeing changed.
> This is good.
>
> Now if I browse to:http://mywebserver/CookieTest/, the same
> index.html file is loaded.
> If I set a cookie and read it, it is changed every time.
> If you view the cookie information (with i.e. FireCookie), you see the
> cookie (in the /CookieTest/) beeing changed.
> This is good.
>

> Now if you go back tohttp://mywebserver/CookieTest, the same

woomla

unread,
Mar 10, 2010, 9:38:14 AM3/10/10
to MooTools Users

On 10 mrt, 14:06, rasmusfl0e <rasmusf...@gmail.com> wrote:
> The MooTools docs state:
>
>  * In order to share the Cookie with pages located in a different
> path, the Cookie.options.domain value must be set.

I don't want to share the Cookie from several paths. What happens here
is that Cookie.write writes to the correct cookie (path: /), but
Cookie.read reads from the incorrect cookie (path: /CookieTest/).

I don't mind adding options, and in fact, by setting the path I'm sure
the cookie is read always correct. This is a good workaround.

I believe that Cookie.read(key) and Cookie.write(key, value) should
both address the same cookie. And they do not!


woomla

unread,
Mar 10, 2010, 9:40:48 AM3/10/10
to MooTools Users

On 10 mrt, 15:38, woomla <woo...@gmail.com> wrote:
> I don't mind adding options, and in fact, by setting the path I'm sure
> the cookie is read always correct. This is a good workaround.

Whoops, not good because Cookie.read doesn't accept options.

woomla

unread,
Mar 10, 2010, 10:38:57 AM3/10/10
to MooTools Users
I was working on this http://mootools.net/shell/b2csd/ (B and D don't
work) and thought path did not work.

But then I realized if I change the path, the document can not see the
cookie.

The problem in my example is this. If I open the page at
http://mywebserver/CookieTest/, with slash, a cookie with path: /
CookieTest/ is written. Document.cookie contains this information.
If I then open the page at http://mywebserver/CookieTest, without the
slash, a cookie with path : / is written. Document.cookie contains
this information also.
Now document.cookie has two cookies with the same name and different
paths. But both cookies are visible to the current page (which is
always http://mywebserver/CookieTest/index.html).

Solution:
1. Use always the slash at the end of the url (how do I force this?)
or
2. Use cookie.write always with path information (/ or /CookieTest/).

I still consider this as a workaround, but the problem might be
related to the way the webbrowser stores the cookie information.

Aaron Newton

unread,
Mar 10, 2010, 11:53:06 AM3/10/10
to mootool...@googlegroups.com
2. Use cookie.write always with path information (/ or /CookieTest/).

This is the correct option in my opinion. 
Reply all
Reply to author
Forward
0 new messages