Writing and reading cookies

Get help on web editors (Frontpage, Dreamweaver) and web languages (HTML, ASP, PHP).
Post Reply
capanno
Registered User
Posts: 5727
Joined: 17 Apr 2004, 02:00
Location: PTA
Contact:

Writing and reading cookies

Post by capanno »

Hi there,

Im pulling my hair out here. I started with cookies today. I write 2 cookies which I need to access from a different window later. In the one page I have the following script:

I call the function "opencomment(title,id)" in the page, and it executes correctly.

Code: Select all

<SCRIPT TYPE="text/javascript">
<!--

function DT_setcookie(name, value) 
{
    document.cookie = name+"="+escape(value);
    return document.cookie;
}

function opencomment(title,id)
{
	var vId = id;
	var vTitle = title; 
	
	DT_setcookie("test_title",vTitle);	
	DT_setcookie("test_id",vId);
}
-->
</SCRIPT>
I then pop up a php file I have on the server. In the php file, I have this script:

Code: Select all

<SCRIPT TYPE="text/javascript">
<!--

function getCookie(name)

{
	var startpos = document.cookie.indexOf(name)+name.length+1;
	var endpos = document.cookie.indexOf(";",startpos)-1;
	if (endpos == -2) endpos = document.cookie.length;
	var result = unescape(document.cookie.substring(startpos,endpos));
	return result;
}


function readcookies()
{
	var vId = getCookie("test_id");
	var vTitle = getCookie("test_title");
}
-->
</SCRIPT>
I got these scripts of the net and I altered them slightly. Now, when I call the function to write the cookie, I return the cookie immediately and this is what I get:

Code: Select all

test_title=cookietitle; test_id=5; __utma=193928944.2115910489.1268903752.1271409901.1272881314.3; __utmz=193928944.1268903752.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)
I see the 2 values I need, so the writing works, kind of. I don't know where the rest comes from. In the browser everything looks fine when I go to cookies in the Options menu. The name, contents, expiry date (at the end of the session), etc.

When I read the cookie with the getCookie function, I get this result:

Code: Select all

193928944.2115910489.1268903752.1271409901.1272881314.
and
928944.2115910489.1268903752.1271409901.1272881314.
Why is this happening? The cooking is still there, I can view it in Options right after the getCookie function was supposed to access it. Any help will be appreciated.
Image
Josh Dies is my hero! |50,000,601.375 forum points
Bladerunner
Registered User
Posts: 14338
Joined: 04 Sep 2004, 02:00
Processor: i386DX Sooper
Motherboard: A blue one
Graphics card: A red one
Memory: Hard drive
Location: On a Möbius strip
Contact:

Re: Writing and reading cookies

Post by Bladerunner »

Why is the startpos in your code the index as well as the length of it? I didn't go through your code thoroughly, but my guess is your value for endpos is incorrect because you're skipping a semicolon due to a wrong startpos.

Try SUBTRACTING 1 from startpos and not adding.

And finally, why if endpos = -2 do you return the whole length? You're assuming if it doesnt find a semicolon, which it probably won't since you skip it, in my opinion, the important cookie data is just the full length of the cookie. I think that is actually the main culprit, but i didn't analyse your code thoroughly as aforementioned.

Get fiirebug and debug that javascript of yours.
If I weren't insane: I couldn't be so brilliant! - The Joker
capanno
Registered User
Posts: 5727
Joined: 17 Apr 2004, 02:00
Location: PTA
Contact:

Re: Writing and reading cookies

Post by capanno »

The start position is at the beginning of the value of the cookie name, name.length+1 skips the name and the = sign that follows it.
I don't think the problem lies there, or anywhere in the reading of the cookie data. When I output the cookie via "alert(document.cookie)" in the function that is called when the page is loaded, I get the following:

__utma=193928944.2115910489.1268903752.1271409901.1272881314.3;
__utmz=193928944.1268903752.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)

The data seems to be missing. Does the fact that I access it from 2 different windows make a difference? Or that the one file is php? Its seems illogical that it should.
Image
Josh Dies is my hero! |50,000,601.375 forum points
Bladerunner
Registered User
Posts: 14338
Joined: 04 Sep 2004, 02:00
Processor: i386DX Sooper
Motherboard: A blue one
Graphics card: A red one
Memory: Hard drive
Location: On a Möbius strip
Contact:

Re: Writing and reading cookies

Post by Bladerunner »

When you use 'document.cookie (somevalue)', does that refer to an attribute - and not the name - of the cookie? It seems that way.

If so, and you have 2 different documents, won't those values not exist for the other document?

Sorry, my javascript isn't good, i'm trying to figure this out logically.
If I weren't insane: I couldn't be so brilliant! - The Joker
capanno
Registered User
Posts: 5727
Joined: 17 Apr 2004, 02:00
Location: PTA
Contact:

Re: Writing and reading cookies

Post by capanno »

I just figured it out!

When the domain attribute is not specified, it stores the domain under the default Host attribute. This is not the same as Domain. I added the domain=url string to the cookie create function, and fixed 1 or 2 bugs in the reading function, and I get the right output now! Much excite! thanks for brainstorming with me!
Image
Josh Dies is my hero! |50,000,601.375 forum points
Post Reply