Solved

How do I read the same cookies in several pages ?

Posted on 2001-07-05
10
257 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to speed up an ASP/MS SQL web application 6 70
Age between date range query (SP) 13 52
Time/Date Query 11 37
Question about ASP Including Files 6 42
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 demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

15 Experts available now in Live!

Get 1:1 Help Now