Why is my cookie not written to disk? (Survey/voting form application)

S

Shannon Jacobs

I'm doing some trivial surveys, and I want to know if the same user
answers twice. Can't really know that, but at least I thought I could
check for the same browser/computer combination by using a cookie.
Here is the code I have now. In the header, I have the following:

<SCRIPT language="JavaScript">
var cookieStatus;
if (document.cookie.length > 0) {
cookieStatus = 'Cookie exists with value ' + document.cookie;
} else {
var deadline = new Date();
deadline.setYear(2003);
deadline.setMonth(7);
deadline.setDate(30);
var cookieValue = Math.random(10000);
document.cookie = "cookieValue=" + cookieValue + "; path=/ ;
expires=" + deadline.toGMTString();
cookieStatus = 'Creating new cookie with value' + cookieValue;
}
</SCRIPT>

What is SUPPOSED to happen is that it should check if the cookie
exists, and if so, just return the value in cookieStatus. If there is
no cookie, it is supposed to create one with a random number and an
expiration date of July 30, 2003.

What is happening now is that it seems to think that the cookie has
been created, and it insists it exists. However, there is no cookie to
be found on disk, and usually the value of the cookie is not what I
thought I set it to be... I feel like I'm missing something very
obvious here....

(On the other side, I have this code near the end of the form:

<script language="JavaScript">
document.writeln ('<INPUT type="hidden" name="cookieValue" value="'+
cookieStatus +'">');
</script>

Near as I can tell, this does its job correctly. The problem seems to
be with getting the correct cookie out to the disk.)

I've read various cookie introductions and explanations, and searched
in various places (like this newsgroup), and the behavior is still
mystifying me. The whole thing is a side-effect of something else?
 
K

kaeli

shanen@my- said:
I'm doing some trivial surveys, and I want to know if the same user
answers twice. Can't really know that, but at least I thought I could
check for the same browser/computer combination by using a cookie.
Here is the code I have now. In the header, I have the following:

<SCRIPT language="JavaScript">
var cookieStatus;
if (document.cookie.length > 0)


I could be wrong, but I think that this will return true if ANY cookies
are there, which is almost always the case.

I use these functions a lot. You're welcome to them if they help.

/* jsCookies.js */
/* This file contains cookie functions. */
/* File Functions:
1. setCookie - writes cookie
2. getCookie - gets value of cookie
3. removeCookie - deletes a cookie
4. detectCookies - checks if cookies are enabled
*/

function setCookie(cookieName, cookieValue, expireDate)
{
/* Pass in three strings - the name of the cookie, the value, and the
expire date.
Pass in a "" empty string for expireDate to set a session cookie
(no expires date).
Pass in any other date for expire as a number of days to be added
to today's date. */

if (expireDate == "")
{
expires = "";
}
else
{
expires = new Date();
expires.setDate(expires.getDate() + expireDate);
expires = expires.toGMTString();
}
document.cookie = cookieName+"="+cookieValue+";expires="+expires;
}

function removeCookie (cookieName)
{
/* Pass in the name of the cookie as a string and it will be removed.
*/
expires = Now();
document.cookie = cookieName+"= ;expires="+expires.toGMTString();
}

function getCookie (cookieName)
{
cookieValue = ""
if (document.cookie.indexOf(cookieName) == -1)
{
// there is no cookie by this name for this user
return cookieValue;
}
else
{
// get the beginning index of the cookie by looking for the cookie
name
cookieStart = document.cookie.indexOf(cookieName);
// get the beginning index of the cookie value by looking for the
equal sign after the name
cookieValStart = (document.cookie.indexOf("=", cookieStart) + 1);
// get the end index of the cookie value by looking for the semi-
colon after the value
cookieValEnd = document.cookie.indexOf(";", cookieStart);
// if no semi-colon, then use the whole length
if (cookieValEnd == -1)
{
cookieValEnd = document.cookie.length
}
// use substring to get the text between the two indices and that
is the value of the cookie
cookieValue = document.cookie.substring(cookieValStart,
cookieValEnd);
return cookieValue;
}
}

function detectCookies()
{
/* function returns true if cookies are enables, false if not */
setCookie("test", "test", "");
tmp = getCookie("test")
if (tmp != "test")
{
return false;
}
else
{
return true;
}
}


-------------------------------------------------
~kaeli~
There is no justification or rationalization
for mutilation. Ban declawing as inhumane.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
var deadline = new Date();
deadline.setYear(2003);
deadline.setMonth(7);

That's August.
deadline.setDate(30);
var cookieValue = Math.random(10000);

Math.random() takes no parameter.
expiration date of July 30, 2003.


Never build a date like that; sometimes, an intermediate stage may be a
non-date. If on 2003 Oct 31 you set
year = 2004 OK 2004 Oct 31
month = 7 !! 2004 Jul 31 -> 2004 Aug 1
date = 22 OK 2004 Aug 22
and you are in the wrong month.

Use
var deadline = new Date("2003/07/30")
or
var deadline = new Date(2003, 6, 30)

These may not be the cause of the problem you currently perceive.
 
R

Randell D.

Shannon Jacobs said:
I'm doing some trivial surveys, and I want to know if the same user
answers twice. Can't really know that, but at least I thought I could
check for the same browser/computer combination by using a cookie.
Here is the code I have now. In the header, I have the following:

<SCRIPT language="JavaScript">
var cookieStatus;
if (document.cookie.length > 0) {
cookieStatus = 'Cookie exists with value ' + document.cookie;
} else {
var deadline = new Date();
deadline.setYear(2003);
deadline.setMonth(7);
deadline.setDate(30);
var cookieValue = Math.random(10000);
document.cookie = "cookieValue=" + cookieValue + "; path=/ ;
expires=" + deadline.toGMTString();
cookieStatus = 'Creating new cookie with value' + cookieValue;
}
</SCRIPT>

What is SUPPOSED to happen is that it should check if the cookie
exists, and if so, just return the value in cookieStatus. If there is
no cookie, it is supposed to create one with a random number and an
expiration date of July 30, 2003.

What is happening now is that it seems to think that the cookie has
been created, and it insists it exists. However, there is no cookie to
be found on disk, and usually the value of the cookie is not what I
thought I set it to be... I feel like I'm missing something very
obvious here....

(On the other side, I have this code near the end of the form:

<script language="JavaScript">
document.writeln ('<INPUT type="hidden" name="cookieValue" value="'+
cookieStatus +'">');
</script>

Near as I can tell, this does its job correctly. The problem seems to
be with getting the correct cookie out to the disk.)

I've read various cookie introductions and explanations, and searched
in various places (like this newsgroup), and the behavior is still
mystifying me. The whole thing is a side-effect of something else?


I'm a bit of a newbie at javascript however I think you might have the
syntax mixed up when creating the cookie - I have a script that I have got
from javascript.internet.com and by reading it, and comparing your script to
it, I believe your cookie should be recorded as follows

cookiename, cookievalue, domain, expiretime, path, secure

I believe all, but the first two are optional. I believe you omitted domain
which is (I believe) required if you are going to put in an expiretime,
path.

If I am correct (that you have omitted domain) then this might explain why
you get unexpected results since your arguements are moving one place to the
left, having javascript take the expiretime as being the domain, and the
path being read as the expiretime.

I am reasonably strong with PHP, and when I use it to plant cookies, I
normally read/write cookies with the basic details (ie cookie name and
cookie value) and nothing else - Once I can write/read back these values,
then I start putting in the extra information (like domain, expiretime,
path, (in)secure).

I hope that helps you.... drop me a byte if you want a copy of a very easy
cookie function I got that would fit your requirements perfectly that I got
from internet.com

laters
randelld
 
S

Shannon Jacobs

<snip, mostly of original code and its minor errors and some of your
comments about possible side effects of statements involving cookies>
I am reasonably strong with PHP, and when I use it to plant cookies, I
normally read/write cookies with the basic details (ie cookie name and
cookie value) and nothing else - Once I can write/read back these values,
then I start putting in the extra information (like domain, expiretime,
path, (in)secure).

I hope that helps you.... drop me a byte if you want a copy of a very easy
cookie function I got that would fit your requirements perfectly that I got
from internet.com

Thanks for the offer of the code, and I'd be interested in seeing it.
I'll also go around and do some more searching for useful code
examples (even though that's where some of the problems with my
original code came from). However, my fuzzy recollection of
internet.com was that it was very annoying in a privacy-intrusive sort
of way, and I'm not sure I'll be going near that particular source...

I've been too busy to put much time into this until today, but so far
my efforts have been rather discouraging. As you mentioned, there seem
to be various peculiar side effects affecting the behavior of cookies.
Other side effects also seem to affect the content of the cookie. For
example, among my various efforts today, I was studying examples of
cookie handling code on some sites that handle cookies, and I decided
on the obvious idea of dumping the cookie back to the Web page and
seeing what it has. Also, I searched the disk and found that cookie,
and opened it up for comparison. For totally mysterious reasons, they
refused to match, and some of the fields in the file version of the
cookie are apparently not visible or accessible in the cookie as the
JavaScript accesses it. Encrypted? Binary fields? But why are they
visible in the editor? I would have thought I had the wrong cookie,
but the time stamps and too many of the fields match up for that
hypothesis...

During my various searches for more information I've found various Web
sites with various kinds of reference information, but so far I
haven't found any explanations that seem to make consistent sense in a
way I can capish... Right now I think I need another little break
before bringing out the heavy artillery: Dr. Pepper.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top