/    Sign up×
Community /Pin to ProfileBookmark

Reading Cookie problem…

Is it possible to read a cookie with javascript that was set by using perl? If I set the cookie using javascript I can read it but I have a serverside program (perl) that sets information that is needed. The interesting thing that if I set a cookie using javascript, the perl program has no trouble reading that cookie. Any help would be appreciated!

Mike

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Arty_EffemOct 29.2006 — Is it possible to read a cookie with javascript that was set by using perl? If I set the cookie using javascript I can read it but I have a serverside program (perl) that sets information that is needed. The interesting thing that if I set a cookie using javascript, the perl program has no trouble reading that cookie.[/QUOTE]Is there any discrepancy/ambiguity in the way the code in both languages is specifying the path parameter?
Copy linkTweet thisAlerts:
@Mike_BurdickauthorOct 29.2006 — Arty,

Thanks for the response!

No, the paths and domains are the same. What's strange is that a cookie set by javascript can be read by the perl script but not the other way around. The cookie to be read by javascript is set like this...

<i>
</i>
function set_cookie ( name, value, exp_d, path, domain, secure ){
var cookie_string = name + "=" + escape ( value );
if ( exp_d ){
var now = new Date();
now.setDate(now.getDate()+ exp_d);
cookie_string += "; expires=" + now.toGMTString();}
if ( path ) cookie_string += "; path=" + escape ( path );
if ( domain ) cookie_string += "; domain=" + escape ( domain );
if ( secure )cookie_string += "; secure";
document.cookie = cookie_string;
}



and read like this...

<i>
</i>function ReadCookie() {
var c_name='User';
if (document.cookie.length&gt;0)
{
var c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
var c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
var cookie_val=unescape(document.cookie.substring(c_start,c_end))
alert(cookie_val);
}
}
return null
}



So until the cookie is put in document.cookie, javascript can't read it!

....at least that's they way I see it! Did I screw up something here?

An internet search turned up others that have the same problem but no one responded to them with a solution.... ?

Thanks
Copy linkTweet thisAlerts:
@Arty_EffemOct 30.2006 — Arty,

Thanks for the response!

No, the paths and domains are the same. What's strange is that a cookie set by javascript can be read by the perl script but not the other way around. [/quote]
Have you tried alerting document.cookie in the relevant folder, and if so do you see a cookie called 'User' (not 'user') ?
Copy linkTweet thisAlerts:
@Mike_BurdickauthorOct 30.2006 — Have you tried alerting document.cookie in the relevant folder, and if so do you see a cookie called 'User' (not 'user') ?[/QUOTE]

Arty,

Yes, I also checked to see if the cookie was set by the perl script and it was. When reading the cookie with the javascript function (see prior post) it contains nothing until I set it with javascript - then it can be read!
Copy linkTweet thisAlerts:
@Arty_EffemOct 30.2006 — Arty,

Yes, I also checked to see if the cookie was set by the perl script and it was. When reading the cookie with the javascript function (see prior post) it contains nothing until I set it with javascript - then it can be read![/QUOTE]
It's strange that if you alert document.cookie it displays the wanted cookie but your function doesn't pick it up.

Could you find a way to capture and post the full content of document.cookie, immediately after the cookie has been set by the perl script?
Copy linkTweet thisAlerts:
@Mike_BurdickauthorOct 30.2006 — It's strange that if you alert document.cookie it displays the wanted cookie but your function doesn't pick it up.

Could you find a way to capture and post the full content of document.cookie, immediately after the cookie has been set by the perl script?[/QUOTE]


Arty,

Thanks for the continued help!

How do I see the full content of document.cookie? What I did was put alerts in the ReadCookie function and checked the output of each step. I knew the cookie was set and what the values were by viewing the cookie from my browser before I executed the function.

Just curious...is it possible to read the perl set cookies with javascript?

Mike
Copy linkTweet thisAlerts:
@Arty_EffemOct 30.2006 — Arty,

Thanks for the continued help!

How do I see the full content of document.cookie? What I did was put alerts in the ReadCookie function and checked the output of each step. I knew the cookie was set and what the values were by viewing the cookie from my browser before I executed the function.

Just curious...is it possible to read the perl set cookies with javascript?

Mike[/QUOTE]
To determine whether JavaScript has a chance of reading the required cookie, I need to see the content of document.cookie as readable by JavaScript.

Somewhere in your document, insert the line:[CODE]document.write(document.cookie);[/CODE]Have the line execute at build-time, then copy the text that it produces and post it here.
×

Success!

Help @Mike_Burdick spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...