Solved

How do I read the same cookies in several pages ?

Posted on 2001-07-05
10
247 Views
Last Modified: 2008-02-20
I have the following dirs and files :

/test1/index.asp
/test2/index.asp
/test1/read.asp
/test2/read.asp

I have this  Response.Cookies("Parent")("name") = "p1" in /test1/index.asp.
I have this  Response.Cookies("Parent")("name") = "p2" in /test2/index.asp.
I have this  Request.Cookies("Parent")("name") in both /test1/read.asp and /test2/read.asp.

When I ran the asp scripts, I discover /test1/read.asp only know how to read the cookies set by
/test1/index.asp. Why ?

How do I modify my scripts, so /test1/read.asp can read the cookies set by /test2/index.asp and
/test2/read.asp can read the cookies set by /test1/index.asp ?

0
Comment
Question by:idontknow12002
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 7

Expert Comment

by:John844
ID: 6255746
make sure that you refreshed your page.
0
 
LVL 7

Expert Comment

by:John844
ID: 6255750
the scenario that you described is exactly what I just did yesterday.  You are able to read the cookie set by the first page on the second page.  I did not see it originally, because my browser cached the page.
0
 
LVL 7

Expert Comment

by:John844
ID: 6255773
the only point that i see that might be causing you a bit of trouble would be the fact that if you set the cookie at the top of a page and try to read the contents of that cookie further down in the same page, you will see the original value, not the value that you are setting it to at the top of the page.


'value of cookie when page loads is "test"
Response.Cookies("Parent")("name") = "p1"

'this will write out "test"
response.write request.Cookies("Parent")("name")


the second time(and so on) the page is executed, it will write out "p1"
0
 

Author Comment

by:idontknow12002
ID: 6255779
I have refreshed so many time.
/test1/read.asp only read the cookies from /test1/index.asp.
0
 
LVL 4

Expert Comment

by:epeele
ID: 6255792
set the default path of the cookie to the root.

Response.Cookies("Parent").path = "/"
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

by:user1234062501
ID: 6255945
I have your answer. Both read.asp scripts CAN read cookies written by both index.asp scripts. If it isn't working, you are doing something that you have not told us.

Are you writing session cookies or cookies with an expires value set?

Also, are you setting any other values for the cookies like domain or path?

If you send me your scripts I can diagnose your problem. I have just finished a project that uses cookies extensively and I have gained an extraordinary understanding of how cookies work.
0
 

Author Comment

by:idontknow12002
ID: 6255981
user1234,

I am very appreciate your help. Can you give me several hours to give you the script ?
0
 
LVL 1

Expert Comment

by:user1234062501
ID: 6255997
BTW, your server will only be able to read the last cookie you wrote using one of your index pages because you gave the cookie the same name. So if you run test1/index.asp you are writing "p1" and with test2/index.asp you are writing "p2" -- to the same cookie. (I don't know if that was obvious or not).
0
 
LVL 1

Expert Comment

by:user1234062501
ID: 6256005
Sure, take your time. I'll help you out. You can email it to me at trw@consultant.com
0
 
LVL 1

Accepted Solution

by:
user1234062501 earned 50 total points
ID: 6257059
Well, there is a minor problem with what you're doing. If you use session cookies you have to be sure to expire the content in your pages immediately. Otherwise the browser will fetch pages from the cache and your cookies won't have any effect.

Example: (This works the way you want)
================================
================================
[test1/index.asp]
<%
Response.Expires = -10000

If Len(Request.QueryString) <> 0 Then

     Response.Cookies("Parent")("name") = ""

Else

     If Len(Request.Cookies("Parent")("name")) = 0 Then
     
          Response.Cookies("Parent")("name") = "p1"
     
     ElseIf Request.Cookies("Parent")("name") <> "p1" Then
     
          Response.Redirect "../test2/index.asp"
     
     End If

End If
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
     <head>
          <title></title>
     </head>
     
     <body>
          <h1>You're on Test1 Index</h1>
          <a href="index.asp?d">delete cookie</a><br>
          test1/index.asp<br>
          <a href="../test2/index.asp">test2/index.asp</a><br>
          <%= "<br><b>Cookie = """ & Request.Cookies("Parent")("name") & """</b>" %>
     </body>
</html>
================================
================================
[test1/index.asp]
<%
Response.Expires = -10000

If Len(Request.QueryString) <> 0 Then

     Response.Cookies("Parent")("name") = ""

Else

     If Len(Request.Cookies("Parent")("name")) = 0 Then

          Response.Cookies("Parent")("name") = "p2"
     
     ElseIf Request.Cookies("Parent")("name") <> "p2" Then
     
          Response.Redirect "../test1/index.asp"
     
     End If

End If
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
     <head>
          <title></title>
     </head>
     
     <body>
          <h1>You're on Test2 Index</h1>
          <a href="index.asp?d">delete cookie</a><br>
          <a href="../test1/index.asp">test1/index.asp</a><br>
          test2/index.asp<br>
          <%= "<br><b>Cookie = """ & Request.Cookies("Parent")("name") & """</b>" %>
     </body>
</html>



If you want users to see the same page across multiple sessions you need to set the expiration for the cookie to whatever length of time you want them to continue seeing the same page.

If you take out the content expiration these pages allow you to type the URL in the browser and retrieve either page after deleting the session cookie.

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now