473,472 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
4 5267
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

"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Wilcox | last post by:
I am using setcookie() to put a cookie on the visitor's machine. When I do so, the cookie is set for that browser session, but not written to disk. What am I missing to write it to the disk? Code...
6
by: Patrick Zittle | last post by:
Does anyone know the code for the cookie monster app? It is a little app that when you start it up it says "I want a cookie" and if you answer anything but "cookie" it asks you agian. If you...
5
by: AHN | last post by:
Please tell me somebody what causes the cookie set with <% Response.Cookies("blah") = "Blah blah" Response.Cookies("blah").Expires = DateAdd( "h", 1, Now() ) %> work as supposed on my local...
7
by: Christoph Pieper | last post by:
Hi, we've the following problem : We have an asp-application which sets the cookie on first login. The cookie will never be touched during user access. The user can work the whole day, but...
5
by: DFS | last post by:
I've written several survey systems in which the majority of the questions have the same or similar responses (Yes/No, True/False, scale of 1 - 5, etc). But this latest survey system I'm working...
2
by: BH | last post by:
I developed a small web app using the FormsAuthentication class to set a cookie (FormsAuthentication.SetAuthCookie(value, isPersist)). The cookie persists fine on my local PC when "isPersist" is...
4
toxicpaint
by: toxicpaint | last post by:
Hi there, I've built a form for peolpe to submit a vote and I thought the best way to restrict people voting twice would be to create a cookie called "voted" and set the value to 1 when they vote....
3
Kelicula
by: Kelicula | last post by:
Hello all! I am getting very close to comleting my recent project thanks to everyones help. I am at a stage where I will start to emplimement end result code. (almost) But I have a problem....
4
by: BritishAgent | last post by:
I'm just starting to learn PHP. I have a website where we are collecting votes for something. I want to use cookies to deter people from re-voting (yes, this is not efficient if they're smart...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.