Solved

How do I read the same cookies in several pages ?

Posted on 2001-07-05
10
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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 have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

762 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