Hi
I am trying to make a cookie based redirect system. The plan was in a 3 page sequence, the first two pages will check if the cookies exist and if they do they will be relocated (replace) to cookie.htm. If there is not a cookie the person will be allowed to view the page. On the last page the JS engine will write a cookie so that the said person can not do the process again. I tested the system out with these codes and either the cookies were never made or the readers did notread the cookies.
EXPIRATION DATE=2 days or Dec 1st 2012 at noon EST
Reader:
Code:var cname = "vote" var data ="1"; var cpath = ""; var cdomain = ""; function CheckForCookie() { if( ExistsCookie(cname) ) { window.location.replace("cookie.htm") } }
Writer:
What am I doing wrong? PLEASE help me!Code:var cname = "vote" var data ="1"; var cpath = ""; var cdomain = ""; // Checks whether the form should be submitted or not // If the cookie exists, the form is not submitted // if no cookie exists, a new cookie, set to expire after // three months ,is written and the form is submitted function CheckCookie() { if( !ExistsCookie(cname) ) { // Write a cookie set to expire after 2 days now= new Date(); // get current date and time expiry = new Date(); expiry.setTime((now.getTime() + 2*24*60*60*1000)); // set to 2 days from now WriteCookie(cname,data,expiry,cpath,cdomain); // Write cookie return true; } alert("You have already submitted this form"); return false; } //Writes the specified data to the cookie file function WriteCookie(name,data,expiryDate,path,domain) { if( name==null || name=="" || data==null || data=="") { return false; } var Cookie = name + "=" + escape(data) + ( (expiryDate)? "; expires=" + expiryDate.toGMTString() : "" ); + ((path)? "; path=" + path : "" ) + ((domain)?"; domain=" + domain : "" ); Cookie +=";"; //Set cookie document.cookie = Cookie; return true; } //Checks if the specified cookie exists or not function ExistsCookie(name) { // cookies are separated by semicolons var aCookie = document.cookie.split("; "); for (var i=0; i < aCookie.length; i++) { // a name/value pair is separated by an equal sign var aCrumb = aCookie[i].split("="); if (name == aCrumb[0]) return true; } // a cookie with the requested name does not exist return false; }


Reply With Quote
Bookmarks