using same cookie server-side and client-side 
Author Message
 using same cookie server-side and client-side

I am trying to use the same cookies on the server-side and the
client-side.  I am creating a multiple-value cookie on the server side
using
        response.cookies("cookie1")("crumb1") = "cookie-value"

and I would like to read that cookie and update it through client-side
code such as
        a = document.cookie
        document.cookie = "crumb2=" & cookie-value2

I can't seem to figure out how the server names the cookie and how I
can read it in on the client.  When I create one on the client, it
does not appear to have a name.

Basically I am just not sure if this can be done at all.

Any help is greatly appreciated!

Thank you,
Parry Starkey



Wed, 01 Sep 2004 06:06:21 GMT  
 using same cookie server-side and client-side
There is no such thing as a server-side cookie. Cookies are always stored on the
client. They can be *written* or *read* on the server, but they still reside on
the client. Any cookie that can be read by the server can also be read on the
client side.

That said, you can certainly read/write them on the client side if you know how.
Here's the MS reference:

http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/...

And here are some functions that you can use to read and write cookies with keys
(the kind you call "multiple value" MS calls "with keys") on the client side.
Note that the addToCookie() function uses a 1-year expiration term. You can make
it whatever you want.

Use getCookie("cookie1","crumb1") for reading the cookie in your example.
Use addToCookie("cookie1","crumb1","cookie-value") to simulate what you did on
the server side

function parseCookie(name) {
  var str = ""
  var a = document.cookie.split(";")
  for (var i=0; i<a.length; i++) {
    a[i] = a[i].trim()
    if (a[i].substring(0,name.length) == name)
      str = a[i].substring(name.length+1)
  }
  var a = str.split("&")
  var obj = new Object()
  if (str.length)
    for (i=0; i<a.length; i++) {
      temp = a[i].split("=")
      obj[temp[0]] = temp[1]
    }
  return obj

Quote:
}

function addToCookie(cookieName,key,value) {
  var cookies = parseCookie(cookieName)
  cookies[key] = value
  var d = new Date()
  d.setFullYear(d.getFullYear()+1)
  var cookieStr = cookieName + "="
  for (var item in cookies)
    cookieStr += item + "=" + cookies[item] + "&"
  cookieStr = cookieStr.substring(0,cookieStr.length-1)
  cookieStr = cookieStr + "; expires=" + d.toUTCString()
  cookieStr = cookieStr + "; path=/"
  document.cookie = cookieStr

Quote:
}

function getCookie(name,key) {
  var cookies = parseCookie(name)
  return cookies[key]

Quote:
}

> I am trying to use the same cookies on the server-side and the
> client-side.  I am creating a multiple-value cookie on the server side
> using
> response.cookies("cookie1")("crumb1") = "cookie-value"

> and I would like to read that cookie and update it through client-side
> code such as
> a = document.cookie
> document.cookie = "crumb2=" & cookie-value2

> I can't seem to figure out how the server names the cookie and how I
> can read it in on the client.  When I create one on the client, it
> does not appear to have a name.

> Basically I am just not sure if this can be done at all.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use of
this email address implies consent to these terms. Please do not contact me
directly or ask me to contact you directly for assistance. If your question is
worth asking, it's worth posting.



Wed, 01 Sep 2004 06:42:09 GMT  
 using same cookie server-side and client-side
I tried this code on the getCookie side and it does not work.  Here is
my server-side code for initially creating the cookie:

Response.Cookies("ctscabs")("adminlevel") = intAdminLevel_Svr
Response.Cookies("ctscabs")("firstname") = strCABSFirstName_Svr
Response.Cookies("ctscabs")("lastname") = strCABSLastName_Svr
Response.Cookies("ctscabs")("uid") = strUID
Response.Cookies("ctscabs").expires = DateAdd("m", 1, Now + 1)

Here is the content of the cookie file after this code:

ctscabs
LASTNAME=Starkey&UID=pss&FIRSTNAME=Parry&ADMINLEVEL=1
cadtest/cabs
0
1216393472
29484968
428197824
29478531
*

When I try getCookie('ctscabs', 'LASTNAME'), the parsecookie routine
fails because after the SPLIT function, the first entry in array a
(a[0]) is this:

LASTNAME=Starkey&UID=pss&FIRSTNAME=Parry&ADMINLEVEL=1

Do I need to create my cookie in the server-side code differently, or
change the parse routine to accomodate the fact that the crumbs are
not separated by ";"?

Thanks!

On Fri, 15 Mar 2002 16:42:09 -0600, "Dave Anderson"

Quote:

>There is no such thing as a server-side cookie. Cookies are always stored on the
>client. They can be *written* or *read* on the server, but they still reside on
>the client. Any cookie that can be read by the server can also be read on the
>client side.

>That said, you can certainly read/write them on the client side if you know how.
>Here's the MS reference:

>http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/...

>And here are some functions that you can use to read and write cookies with keys
>(the kind you call "multiple value" MS calls "with keys") on the client side.
>Note that the addToCookie() function uses a 1-year expiration term. You can make
>it whatever you want.

>Use getCookie("cookie1","crumb1") for reading the cookie in your example.
>Use addToCookie("cookie1","crumb1","cookie-value") to simulate what you did on
>the server side

>function parseCookie(name) {
>  var str = ""
>  var a = document.cookie.split(";")
>  for (var i=0; i<a.length; i++) {
>    a[i] = a[i].trim()
>    if (a[i].substring(0,name.length) == name)
>      str = a[i].substring(name.length+1)
>  }
>  var a = str.split("&")
>  var obj = new Object()
>  if (str.length)
>    for (i=0; i<a.length; i++) {
>      temp = a[i].split("=")
>      obj[temp[0]] = temp[1]
>    }
>  return obj
>}

>function addToCookie(cookieName,key,value) {
>  var cookies = parseCookie(cookieName)
>  cookies[key] = value
>  var d = new Date()
>  d.setFullYear(d.getFullYear()+1)
>  var cookieStr = cookieName + "="
>  for (var item in cookies)
>    cookieStr += item + "=" + cookies[item] + "&"
>  cookieStr = cookieStr.substring(0,cookieStr.length-1)
>  cookieStr = cookieStr + "; expires=" + d.toUTCString()
>  cookieStr = cookieStr + "; path=/"
>  document.cookie = cookieStr
>}

>function getCookie(name,key) {
>  var cookies = parseCookie(name)
>  return cookies[key]
>}


>> I am trying to use the same cookies on the server-side and the
>> client-side.  I am creating a multiple-value cookie on the server side
>> using
>> response.cookies("cookie1")("crumb1") = "cookie-value"

>> and I would like to read that cookie and update it through client-side
>> code such as
>> a = document.cookie
>> document.cookie = "crumb2=" & cookie-value2

>> I can't seem to figure out how the server names the cookie and how I
>> can read it in on the client.  When I create one on the client, it
>> does not appear to have a name.

>> Basically I am just not sure if this can be done at all.

>--
>Dave Anderson

>Unsolicited commercial email will be read at a cost of $500 per message. Use of
>this email address implies consent to these terms. Please do not contact me
>directly or ask me to contact you directly for assistance. If your question is
>worth asking, it's worth posting.



Fri, 03 Sep 2004 21:53:54 GMT  
 using same cookie server-side and client-side
On my last post, I messed up the entry in a[0] after the split
function.  It should be:

ctscabs=LASTNAME=Starkey&UID=pss&FIRSTNAME=Parry&ADMINLEVEL=1

The line a[i] = a[i].trim() fails with a "object doesn't support this
property or method" message
============================

On Fri, 15 Mar 2002 16:42:09 -0600, "Dave Anderson"

Quote:

>There is no such thing as a server-side cookie. Cookies are always stored on the
>client. They can be *written* or *read* on the server, but they still reside on
>the client. Any cookie that can be read by the server can also be read on the
>client side.

>That said, you can certainly read/write them on the client side if you know how.
>Here's the MS reference:

>http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/...

>And here are some functions that you can use to read and write cookies with keys
>(the kind you call "multiple value" MS calls "with keys") on the client side.
>Note that the addToCookie() function uses a 1-year expiration term. You can make
>it whatever you want.

>Use getCookie("cookie1","crumb1") for reading the cookie in your example.
>Use addToCookie("cookie1","crumb1","cookie-value") to simulate what you did on
>the server side

>function parseCookie(name) {
>  var str = ""
>  var a = document.cookie.split(";")
>  for (var i=0; i<a.length; i++) {
>    a[i] = a[i].trim()
>    if (a[i].substring(0,name.length) == name)
>      str = a[i].substring(name.length+1)
>  }
>  var a = str.split("&")
>  var obj = new Object()
>  if (str.length)
>    for (i=0; i<a.length; i++) {
>      temp = a[i].split("=")
>      obj[temp[0]] = temp[1]
>    }
>  return obj
>}

>function addToCookie(cookieName,key,value) {
>  var cookies = parseCookie(cookieName)
>  cookies[key] = value
>  var d = new Date()
>  d.setFullYear(d.getFullYear()+1)
>  var cookieStr = cookieName + "="
>  for (var item in cookies)
>    cookieStr += item + "=" + cookies[item] + "&"
>  cookieStr = cookieStr.substring(0,cookieStr.length-1)
>  cookieStr = cookieStr + "; expires=" + d.toUTCString()
>  cookieStr = cookieStr + "; path=/"
>  document.cookie = cookieStr
>}

>function getCookie(name,key) {
>  var cookies = parseCookie(name)
>  return cookies[key]
>}


>> I am trying to use the same cookies on the server-side and the
>> client-side.  I am creating a multiple-value cookie on the server side
>> using
>> response.cookies("cookie1")("crumb1") = "cookie-value"

>> and I would like to read that cookie and update it through client-side
>> code such as
>> a = document.cookie
>> document.cookie = "crumb2=" & cookie-value2

>> I can't seem to figure out how the server names the cookie and how I
>> can read it in on the client.  When I create one on the client, it
>> does not appear to have a name.

>> Basically I am just not sure if this can be done at all.

>--
>Dave Anderson

>Unsolicited commercial email will be read at a cost of $500 per message. Use of
>this email address implies consent to these terms. Please do not contact me
>directly or ask me to contact you directly for assistance. If your question is
>worth asking, it's worth posting.



Fri, 03 Sep 2004 21:58:49 GMT  
 using same cookie server-side and client-side
Sorry - I forgot that this is in one of my standard includes:

String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "")}

Quote:

> On my last post, I messed up the entry in a[0] after the split
> function.  It should be:

> ctscabs=LASTNAME=Starkey&UID=pss&FIRSTNAME=Parry&ADMINLEVEL=1

> The line a[i] = a[i].trim() fails with a "object doesn't support this
> property or method" message
> ============================

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use of
this email address implies consent to these terms. Please do not contact me
directly or ask me to contact you directly for assistance. If your question is
worth asking, it's worth posting.



Sat, 04 Sep 2004 00:02:58 GMT  
 using same cookie server-side and client-side
To add to Dave's posting, and to help make it clearer about cookies, the
following TWO cookie statements access the SAME cookie on the Clients
MACHINE, NOT server cookie.  If you want server cookies, look into Session
variables (similar to cookies but for the server).

: Example :

<%
  Response.Cookie("MyCookie") = "Test"
%>

<script language="VBScript">
  Document.Cookie("MyCookie") = "Test"
</script>

As previously mentioned, the MyCookie cookie accessed using Server Side code
is actually the exact same variable as the MyCookie cookie on the Client
Side.  So I did the Following:

<% Response.Cookie("MyCookie") = "Test" %>

<script language="VBScript">
  MsgBox Document.Cookie("MyCookie")
</script>

The value the msgbox pops up would be "Test".

Hope it helped :)

Mythran



Sat, 04 Sep 2004 01:44:01 GMT  
 using same cookie server-side and client-side
Mythran,

When I try this, on the client script side, I get the following error:

Object doesn't support this action: 'Document.Cookie'

This is what was happening to me when I started this thread :)  The
cookie looks like it was created OK as I can see it in my temp
internet files folder.

Also, as a side question, can you explain why the cookie on my
harddrive is named "psstest" ?  psstest is the name of my web - I
wondered why most cookies in my temp internet folder are named

Thanks so much!

On Mon, 18 Mar 2002 09:44:01 -0800, "Mythran"

Quote:

>To add to Dave's posting, and to help make it clearer about cookies, the
>following TWO cookie statements access the SAME cookie on the Clients
>MACHINE, NOT server cookie.  If you want server cookies, look into Session
>variables (similar to cookies but for the server).

>: Example :

><%
>  Response.Cookie("MyCookie") = "Test"
>%>

><script language="VBScript">
>  Document.Cookie("MyCookie") = "Test"
></script>

>As previously mentioned, the MyCookie cookie accessed using Server Side code
>is actually the exact same variable as the MyCookie cookie on the Client
>Side.  So I did the Following:

><% Response.Cookie("MyCookie") = "Test" %>

><script language="VBScript">
>  MsgBox Document.Cookie("MyCookie")
></script>

>The value the msgbox pops up would be "Test".

>Hope it helped :)

>Mythran



Sat, 04 Sep 2004 04:36:16 GMT  
 using same cookie server-side and client-side
Two words: case sensitivity.

Try document.cookie, not Document.Cookie

Quote:

> Object doesn't support this action: 'Document.Cookie'

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use of
this email address implies consent to these terms. Please do not contact me
directly or ask me to contact you directly for assistance. If your question is
worth asking, it's worth posting.



Sat, 04 Sep 2004 07:11:51 GMT  
 using same cookie server-side and client-side

Quote:
> Two words: case sensitivity.

> Try document.cookie, not Document.Cookie


> > Object doesn't support this action: 'Document.Cookie'

VBScript is not case sensitive...  The problem is that on the client side, document.cookie is just a delimited string, not a collection.  You have to parse it yourself...

<% Response.Cookies("MyCookie") = "Test" %>

<script language="VBScript">
  myCookies = split(Document.Cookie,"; ")
  for each crumb in myCookies
    temp = split(crumb,"=")
    document.write "[cookie name] " & temp(0)
    document.write " [value] " & temp(1)
    document.write "<br>"
  next
</script>

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US
--



Sun, 05 Sep 2004 00:28:33 GMT  
 using same cookie server-side and client-side

Quote:

> VBScript is not case sensitive...

My mistake - I forgot where I was.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use of
this email address implies consent to these terms. Please do not contact me
directly or ask me to contact you directly for assistance. If your question is
worth asking, it's worth posting.



Sun, 05 Sep 2004 00:36:55 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Client side or server side cookie

2. Client-side Recordset using Server-side DSN?

3. Using Server side Vs OnClick Client side

4. attached Contact Item on server-side becomes attached Mail Item on client-side

5. How Can I Pass Data Between the Server Side and the Client Side VB/JScript

6. Server side or client side cursors?

7. Printing report at the client side and the report is at the server side

8. Printing report at the client side and the report is at the server side

9. Mixing of client-side and server-side scripts

10. Server side variables to client side script?

11. Server-Side vs Client-Side script

12. Client Side + Server Side Scripts

 

 
Powered by phpBB® Forum Software