By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,077 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,077 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <c5*************************@posting.google.com> , shanen@my-
deja.com enlightened us with...
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
-------------------------------------------------
Jul 20 '05 #2

P: n/a

"Shannon Jacobs" <sh****@my-deja.com> wrote in message
news:c5*************************@posting.google.co m...
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
Jul 20 '05 #3

P: n/a
JRS: In article <c5*************************@posting.google.com> , seen
in news:comp.lang.javascript, Shannon Jacobs <sh****@my-deja.com> posted
at Mon, 14 Jul 2003 18:16:44 :-
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #4

P: n/a
"Randell D." <yo**************************@yahoo.com> wrote in message news:<K1***********************@news2.calgary.shaw .ca>...
"Shannon Jacobs" <sh****@my-deja.com> wrote in message
news:c5*************************@posting.google.co m...
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:

<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.
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.