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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookie Problems

Status
Not open for further replies.

Jonnn

Programmer
Nov 17, 2004
135
GB
I have been working on a cookie ...

When you go on my website for the first time it will ask you to select your profile. After doing so, it saves a cookie with the name of your profile inside it.

Everytime you go back to the page, it checks whether or not the cookie with your profile name exists or not, if it does it will take you to the main page, if it doesnt, it will take you to the profile selection page.

My cookie works fine for a couple of days. I can turn my computer off and on and when i go back to the website, it will always remember my name.

Until i delete my cookies ...

After a few days though, my cookie doesnt work anymore, it works if you swap webpages with your browser open. But as soon as i close my browser, it wont work!.

I have checked my expiry date on my cookie (which i have set to one year).

Im just wondering if any of you have had similar problems.

My website is
i dont know whether freewebs may have something to do with it?

Here is my cookie saving code by the way if it helps ...

Code:
function confirmProfile() {

selectedNumber = document.getElementById("choosePlayer").selectedIndex

selectedPlayer = document.form1.choosePlayer.options[selectedNumber].name

expireTime = new Date(); // create a new blank date

oneYear = 1000*60*60*24*7*52 // 1000 milliseconds in a second. 1000 * 60 = 1min, *60 = 1hour, *24 = 1day * 53 = 1 year
alert(oneYear)
expireTime.setTime(expireTime.getTime() + oneYear); // set the time of blank date to the current date + 1day

expireTime.toGMTString(); // convert the time into G.M.T Time

// create the cookie using the following: - 1)name of cookie, 2)contents of cookie, 3)expiry date of cookie

document.cookie = selectedPlayer + ";" + "expires=" + expireTime + ";"

alert("Thank you, you will now be redirected to the main page" + document.cookie);

window.location="[URL unfurl="true"]http://www.freewebs.com/foxandhounds/index.html";[/URL]
}

I hope someone can help me in this strange problem ...

Thanks alot

- Jon

JavaScript Beginner at work :)
 
anyone???

JavaScript Beginner at work :)
 
Jonnn, I'm not sure exactly why your cookies aren't working, but you might find this helpful. I didn't write these 2 functions, I got them somewhere off the internet a long time ago and I don't remember where. However, any time I've used cookies it's been with these 2 functions, and they always work correctly. Here's a remember login test I wrote up, maybe it'll help you out:
Code:
<script language=javascript>
function getCookie(name) {
   var start = document.cookie.indexOf(name+"=");
   var len = start+name.length+1;
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(len,end));
}

function setCookie(name,value,expires,path,domain,secure) {
   document.cookie = name + "=" +escape(value) +
   ((expires) ? ";expires=" + expires.toGMTString() : "") +
   ((path) ? ";path=" + path : "") + 
   ((domain) ? ";domain=" + domain : "") +
   ((secure) ? ";secure" : "");
}

</script>

<body>
<form name=blahForm>
<script language=javascript>
var blah = getCookie("cookieTest");
var expireDate = new Date();
if (blah != null) {
   document.write("Welcome back " + blah + "<br><br>");
   expireDate.setTime(expireDate.getTime() - 1);
   document.write("<input type=button value='Destroy Cookie' onclick='setCookie(\"cookieTest\", \"\", expireDate);alert(\"cookie erased\");'><br><br>");
}
else {
   document.write("<input type=text name=blahText><br><br>");
   expireDate.setTime(Date.parse("01/06/2070"));
   document.write("<input type=button value='Remember Me' onclick='setCookie(\"cookieTest\", blahForm.blahText.value, expireDate);alert(\"cookie saved\");'><br><br>");
}
</script>
<input type=button value='Refresh Page' onclick='window.location.reload()'>
</form>
</body>

-kaht

Do the chickens have large talons?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top