Solved

How do I read the same cookies in several pages ?

Posted on 2001-07-05
10
272 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Title # Comments Views Activity
SP to delete duplicates 15 73
Need help editing script 3 78
age from date of birth 4 51
Jquery Check against database on select from auto fill not working in all browsers 5 41
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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/…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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