Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some cookie help, please 1

Status
Not open for further replies.

greedyzebra

Technical User
Sep 5, 2001
140
0
0
US
My problem boils down to this. Why do I have a cookie that exists on only one page? Here are the details:

1) We start at the index page. onload we perform a function called "sniff" that creates a cookie that stores some environmental data (screen size, browser type). This has always created successfully. For what it's worth this is an .shtml page.

2) We move to another page where the "sniff" function is attempted again, but since the cookie it's meant to create already exists, it does nothing. For what it's worth, this too is an .shtml page.

3) We move to the third page (the problem page). I have an alert set up on entry to this and each of the pages accessed in the first two steps. In all cases, including now, the environmental cookie exists. When I initiate the action to create the new cookie (all this cookie stores is comma delimited numbers) it gets created and is visible with an appropriately placed alert. If I want I can then exit to the next page (whose output/displayed html is based upon the cookie data), or I can click on a few more things to add to the cookie. In either case the results are the same... I have an alert setup on "onunload" and the cookie and its data exists. For what it's worth this page is entirely generated by a Perl script in cgi-bin. That is, its url is For future reference, the name of this cookie is "rq".

4) We move to the fourth page which is, again, a .shtml file with the ssi call being to a Perl script that grabs the data for the cookie that was created on the previous page (in the previous step). I have the script print out all cookies in html comments. The enviornmental cookie is there, but the cookie created in step three is not. Nor does it show up in the onload-generated alert.

5) I can travel around to other pages within my site and the cookie generated in step 3 is never seen by the alerts. But the enviornmental cookie (which even though the function to create is called on every page, is still created only in step 1) is there. If I go back to the page where my "user-defined" cookie was created in step three, it shows up in the alert!

So, again, the bottom line is that the cookie is only visible on the page where it was created.

Some random notes:
*The same function is used to create the enviornmental cookie and the "step 3" cookie.
*I've modified the "sniff" function that's called on each page to create a non-valued "rq" cookie. This cookie without value can be seen everywhere the environmental cookie can be seen. When I supposedly add to the cookie in step 3, two cookies named "rq" exist in the document.cookie alert. However, only the non-valued "rq" exists everywhere.
*The cookie data is "escaped" before being added to document.cookie
*The cookies are good only for the session so the only data that exists in them are the name and its subsequent values. So it's not an expiration issue or a domain issue.
*The code for the cookies has worked before. I have actually made no modifications on the particular functions dealing with the cookie creation and editing. I have made changes to the script that produces the page in step 3, but there is no difference in the function call to the "setCookie" function.

I am delighted to post code if necessary, but given how long-winded this explanation is so far, I don't want to make it longer and turn away any potential helpers. So if you need it, I'm happy to post it. I've worked on this for a couple of days now, obviously without success, so any suggestions are greatly appreciated!

Thanks!
 
Thanks for the *very* detailed explanation... considering I'm the first to respond (and it is about to clock on over to the 15th shortly) I'd say that you have turned away any potential helpers with the description.

Could you post your code or post a reference to a URL?

Pete.


Web Developer & Aptrix / Lotus Workplace Web Content Management (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
My guess is the cookie set in 3rd page has a different URL path to the 4th page which read the cookie. Cookie is only accessible for URLs with the same URL path or sub-path.

The path attribute specifies a subset of URLs in a domain for which a cookie is valid.

Let say the URL for the page 1 to 4 are as follows:
1. (cookie1)
2. (cookie2)
3. 4.
If "path" attribute is not specified when the cookie is set in page 2. Then page 3 can access cookie1 but not cookie2.

If this is the case, the solution would be set path attribute to "/" when setting the cookie in page 2.

e.g.
document.cookie = name + "=cookieName;path=/;expires=-1";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top