Jump to content

My cookie doesn't expire


MarcP

Recommended Posts

I have a test cookie that I set to expire in one day and here it is 3 days later and it still hasn't expired.Cookies confuse me and I could use the help, thank you,Here's the cookieM~

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">  <title>Cookie1</title>  <script type="text/javascript">function getCookie(c_name){if (document.cookie.length>0){c_start=document.cookie.indexOf(c_name + "=");if (c_start!=-1){ c_start=c_start + c_name.length+1 ;c_end=document.cookie.indexOf(";",c_start);if (c_end==-1) c_end=document.cookie.lengthreturn unescape(document.cookie.substring(c_start,c_end));} }return ""}function setCookie(c_name,value,expiredays){var exdate=new Date();exdate.setDate(exdate.getDate()+1000*60*60*24*1);document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());}function checkCookie(){cftrial=getCookie('cftrial');if (cftrial!=null && cftrial!=""){alert('Welcome again '+cftrial+' !');}else {cftrial=prompt('Thank you for trying my software for a 30 day trial, Please enter your NAME to continue:',"");if (cftrial!=null && cftrial!=""){setCookie('cftrial',cftrial,1);}}}  </script></head><body onload="checkCookie()"><br></body></html>

Link to comment
Share on other sites

Are you sure you're setting the right expiration date? Can you find the cookie saved on your computer and look at it?
Thank you for your quick response, I've found the cookie and it says that the cookie expires when the session ends. I'm assuming that means when I close the browser.M~
Link to comment
Share on other sites

Yeah, session cookies get deleted when the browser closes.
yeah but according to the code above shouldn't the cookie expire in 1 day? I set that cookie over 5 days ago and it still hasn't expired.My ultimate goal is to have a cookie expire in a given amount of days at which point the user will be re-directed to another page.I'm finding cookies to be a bit confusing. I can't find a problem with the above code, any ideas justsomeguy?M~
Link to comment
Share on other sites

yeah but according to the code above shouldn't the cookie expire in 1 day?
It's been a long time since I had to create a cookie in javascript, but I think you're setting your cookie to expire in 86,400,000 days.This is what you have:
function setCookie(c_name,value,expiredays){var exdate=new Date();exdate.setDate(exdate.getDate()+1000*60*60*24*1);document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());}

This is what you should have:

function setCookie(c_name,value,expiredays){var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());}

Link to comment
Share on other sites

  • 3 weeks later...
It's been a long time since I had to create a cookie in javascript, but I think you're setting your cookie to expire in 86,400,000 days.This is what you have:
function setCookie(c_name,value,expiredays){var exdate=new Date();exdate.setDate(exdate.getDate()+1000*60*60*24*1);document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());}

This is what you should have:

function setCookie(c_name,value,expiredays){var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());}

Thank you jesh, during the bandwidth blackout I was able to figure it out.I am now trying to re-direct to another html page when the cookie expires, any ideas?M~
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...