Is it possible to use MachineName or IP in cookie's domain parameter?

A

alberthung01

Hi all,
I wrote the code below in client script block,
but I couldn't retrieve the cookie which was set in line 1.

Server1
processCookies.htm
url->http://Server1/mainSite/processCookies.aspx
------------------
document.cookie="myName=Test;domain=Server1";
alert(document.cookie);
------------------

My purpose is to share cookies between two or more web servers in my
LAN.
So if it works,I will change the domain value to Server2,
Server3...etc. in the main site processCookies page.
And then when users link to other site the cookie will also available.
Whether domain doesn't accept this kind format?

Thanks,
Albert
 
M

Michael Nemtsev

Hello (e-mail address removed),

";" sign means the end of cookie value, so your server value is not stored,
AFAIK

I'd recommend to use the following codesnippet to store / extract cookies

/ primary function to retrieve cookie by name
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 "";
}

// store cookie value with optional details as needed
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}



---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
 
A

alberthung01

Michael,Alex,
Thanks for your help and information!
Now I can set cookie successfully as long as I don't specify the
computer name as domain.
And I found the article describe which are invalid domain value.
http://www.ietf.org/rfc/rfc2109.txt
* The value for the Domain attribute contains no embedded dots or
does not start with a dot.

* The value for the request-host does not domain-match the Domain
attribute.

So the way I try to use to share cookies between diffenent web server
is impossible.
I'll try another way,thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top