PDA

View Full Version : GetCookie from another domain


Jerome
10-21-2002, 08:16 PM
Hi,

I have 2 domain adresses e.g. www.domain1.com and www.domain2.com

At domain1 I set a cookie, see below:

function SetCookie(name,value){
var argv=SetCookie.arguments;
var argc=SetCookie.arguments.length;
var expires=(argc>2)?argv[2]:null;
var path=(argc>3)?argv[3]:null;
var domain=(argc>4)?argv[4]:null;
var secure=(argc>5)?argv[5]:false;
document.cookie=name+"="+escape(value)+((expires==null)?"":(";expires="+expires.toGMTString()))+((path==null)?"":(";path="+path))+((domain==null)?"":(";domain="+domain))+((secure==true)?";secure":"");
}

From domain1 I link to domain2 and I want to get the cookie set at domain1, see below:

function GetCookie(name){
var arg=name+"=";
var alen=arg.length;
var clen=document.cookie.length;
var i=0;while(i<clen){
var j=i+alen;
if(document.cookie.substring(i,j)==arg)
return getCookieVal(j);
i=document.cookie.indexOf(" ",i)+1;
if(i==0)break;
}
return null;
}

1. Is it possible?
2. Is it possible to set more than one cookie in the same time for every adress/domain?
3. Can anybody make it possible for me?

Thanks in advance,
Jerome

beetle
10-21-2002, 09:27 PM
I found some resources on this, but done with ASP, not javascript...

Jerome
10-22-2002, 08:26 AM
I like to have it done in javascript e.g. client-side....

glenngv
10-22-2002, 08:43 AM
theoretically, you can't access a cookie from another domain, or else, a particular site could freely get the all the cookies set by any site in the user's machine! that spells a lot of trouble.

what you could do is put the cookies in hidden fields then submit the form in another domain which reads the form data and then save them in its own cookie

beetle
10-22-2002, 08:51 AM
I was just going to say....

There's got to be security issues involving JS cookies that prevent you from reading across domains...

glenngv
10-22-2002, 08:53 AM
just curious. where did you see the ASP resources you were talking about?

beetle
10-22-2002, 08:57 AM
Bunch here...

http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=sharing+cookies+across+domains

glenngv
10-22-2002, 09:26 AM
i think, what was in the search results were either "techniques" in sharing cookies across domains (maybe something like the one I posted earlier) or sharing cookies across subdomains.

So technically, you can't directly (as you would in a same domain) read cookies from another domain using either Javascript or any server-side language.

Jerome
10-22-2002, 10:57 AM
What I understand, there should be a possibillity using - sub-domains - f.e.

SetCookie in www.domain1.com

GetCookie in sub1.domain1.com

Or did I understand it wrong?

By the way thanks for the replies,
Jerome

ConfusedOfLife
10-22-2002, 02:44 PM
No, you're right.
If the domain attribute matches the end of the fully qualified domain name of the host, then path matching is performed to find out if the cookie should be sent. For example a domain attribute of "something.net" would math "one.something.net" and "one.two.something.net".
Only hosts within the specified domain can set a cookie for that domain.

Jerome
10-22-2002, 02:54 PM
Thanks!

This will do the trick...

Jerome