473,499 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting local cookie

I'm trying to store a value in a cookie but its not working. Can
anyone see what I might be doing wrong.

Thanks,
Chris
~~~~~~~~~~~~~~~~~~
<script language="javascript">
if(document.cookie.indexOf("beenHere1=true")!=-1)

else
{
var ExpirationDate = new Date();
var oneDayFromNow = exp.getTime() + (1 * 24 * 60 * 60 * 1000);
exp.setTime(oneDayFromNow);
document.cookie = "beenHere1=true; ";
document.cookie = document.cookie + "stnum=" +
$query->param('-stnum'); + "; ";
document.cookie = document.cookie + "stnm=" +
$query->param('~stnm'); + "; expires=" + exp.toGMTString();
}

</script>

Jul 23 '05 #1
4 3787
<so**********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I'm trying to store a value in a cookie but its not working. Can
anyone see what I might be doing wrong.

Thanks,
Chris
~~~~~~~~~~~~~~~~~~
<script language="javascript">
if(document.cookie.indexOf("beenHere1=true")!=-1)

else
{
var ExpirationDate = new Date();
var oneDayFromNow = exp.getTime() + (1 * 24 * 60 * 60 * 1000);
exp.setTime(oneDayFromNow);
document.cookie = "beenHere1=true; ";
document.cookie = document.cookie + "stnum=" +
$query->param('-stnum'); + "; ";
document.cookie = document.cookie + "stnm=" +
$query->param('~stnm'); + "; expires=" + exp.toGMTString();
}

</script>


Where is "exp" declared?

Are you using PHP inside JavaScript?

What does the following do?
$query->param('-stnum');
$query->param('~stnm');

You should build the value of a variable and set "document.cookie = " once.

For example,

<script type="text/javascript">
if (document.cookie.indexOf("beenHere1=true") < 0) {
var now = new Date();
var exp = new Date(now.getTime() + (1 * 24 * 60 * 60 * 1000));
document.cookie = "beenHere1=true!;expires=" + exp.toGMTString();
}
</script>
Jul 23 '05 #2
In article <11**********************@z14g2000cwz.googlegroups .com>,
so**********@yahoo.com wrote:
I'm trying to store a value in a cookie but its not working. Can
anyone see what I might be doing wrong.


Here is a copy of my earlier post. Pick a working cookie routine.

Robert

Here are my suggestions.

1) Always set the expiration time. IE works better.

2) You may have disabled cookies. In IE, I had to go to the site then
check whether or not cookies were enabled.

3) Maybe your cookie rountines could use improvements.

I suggest using an existing set of cookie routines. The
document.cookie can contain existing cookies and you need to preserve
them. ( Don't know why such routines aren't shipped with the browsers.
)

~kaeli~ has developed some cookie code. You may view it in this
article:

http://groups.google.com/groups?hl=e...SO-8859-1&selm...
..1af96c2a4f8abc94989dcc%40nntp.lucent.com

Also, ~kaeli~ reports that:
There's some even better code that sets more cookie properties like
domain, secure, path, etc here.
http://www.acm.uiuc.edu/webmonkeys/j...ipt/cookies.js

4) I wrote a short program for verifying cookies will work on a site.
The cookie code was taken from:
Functions from Using HTML 4, Java 1.1, and JavaScript 1.2:
Complete. Definitive. Invaluble.
by Eric Jim O'Donnell, et al.
and from the Netscape develper's site. Here is my cookie testing
code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>Remember by cookie</title>

<script type="text/javascript">

// Sets cookie values. Expiration date is optional
//
function setCookie(name, value, expire) {
document.cookie = name + "=" + escape(value)
+ ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))

}

function getCookie(Name) {
var search = Name + "="
if (document.cookie.length > 0) { // if there are any cookies
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset)
// set index of end of cookie value
if (end == -1)
end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
}

}

function deleteCookie(name,path,domain) {
if (getCookie(name)) document.cookie = name + "=" +
( (path) ? ";path=" + path : "") +
( (domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-70 00:00:01 GMT";

}

function registerCookie(cookieName,cookieValue) {
var today = new Date()
var expires = new Date()
expires.setTime(today.getTime() + 1000*60*60*24*365)
setCookie(cookieName, cookieValue, expires)
}

</script>

<body>
<p>Let's see if we can set then read a cookie

<script type="text/javascript">
document.write("when on (document.url) " + document.URL);
document.write("</p>");

var all = "I hope this cookie doesn't crumble.";

document.write("<p>");
document.write("document.cookie before deleteCookie = "
+ document.cookie + "<br><br>");
deleteCookie("mycookietry");
document.write("document.cookie after deleteCookie= "
+ document.cookie + "<br><br>");

registerCookie("mycookietry",all);

document.write("document.cookie after registerCookie = "
+ document.cookie + "<br><br>");

theMessage = getCookie("mycookietry");
document.write("theMessage = " + theMessage + "<br>");
document.write("</p>");
</SCRIPT>
</body>
</html>
Jul 23 '05 #3
JRS: In article <lN********************@comcast.com>, dated Wed, 26 Jan
2005 07:22:08, seen in news:comp.lang.javascript, McKirahan
<Ne**@McKirahan.com> posted :

var now = new Date();
var exp = new Date(now.getTime() + (1 * 24 * 60 * 60 * 1000));
document.cookie = "beenHere1=true!;expires=" + exp.toGMTString();

var D = new Date();
D.setDate(D.getDate()+1)
document.cookie = "beenHere1=true!;expires=" + D.toGMTString();

is smarter; if a day is called for, give a civil day, so that it expires
at the same time next day.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4
JRS: In article <rc*****************************@individual.net> , dated
Wed, 26 Jan 2005 13:00:41, seen in news:comp.lang.javascript, Robert
<rc*******@my-deja.com> posted :

function registerCookie(cookieName,cookieValue) {
var today = new Date()
var expires = new Date()
expires.setTime(today.getTime() + 1000*60*60*24*365)
setCookie(cookieName, cookieValue, expires)
}

function registerCookie(cookieName,cookieValue) {
var D = new Date()
D.setMonth(D.getMonth()+12)
setCookie(cookieName, cookieValue, D)
}

is smarter; if a year is called for, give 12 months, and it will expire
at the same date next year regardless of the Leap Year situation -
except when called on Feb 29th, of course, when it gives Mar 1st.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5456
by: grz02 | last post by:
A question on html-forms: We have an application where users will get a CD with, among other things, some html-forms for them to fill in. When they click the submit-button, the data is...
2
9157
by: bagsmode | last post by:
Hi, I'm trying to set a session cookie and then redirect, however I get the error: Status: 302 Moved Location: /index.cgi I thought I recall getting an error like this when I first tried...
2
2278
by: Thomas Krebs | last post by:
Is it possible to set a cookie in a HTML file which is loaded from the local filesystem (protocol is 'file:' instead of 'http')? It seems that I can set the cookie and I am also able to access it...
1
3447
by: Jose Olivas | last post by:
I am setting a cookie on a subdomain: http://store1.mydomain.com Then the store takes me to a shopping cart that is at: http://shopping.mydomain.com Somewhere the following code (taken and...
4
2218
by: Rickey Tom | last post by:
This has to be a very common question, but my search did not come up with an answer. I needed to set an expiration time for a cookie. In .NET, is seems that the server-side code is used to set...
2
2554
by: junlia | last post by:
Hi All, I am working on a project that acts as a bridge. It does some checking with post xml data, and then redirects the request to an appropriate page. However, we find that depends on the...
1
6436
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
4
4105
by: gcervantes | last post by:
Hello, I am making a screen scraper, in other programs I have been able to log into a site, get the cookie, submit a form and read the results. However, this time I am in troubles, the site I need...
4
7297
by: SevDer | last post by:
Hi, I've done some coding in my web application however right now for an unknown reason my asp.net 2.0 site is not setting asp.net_sessionid cookie and as a result, I am losing the session data...
0
7012
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
7180
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
6901
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
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4920
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
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.