www.webdeveloper.com
Results 1 to 2 of 2

Thread: Cookie Redirect

  1. #1
    Join Date
    Nov 2012
    Posts
    1

    Question Cookie Redirect

    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:
    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;
    }
    What am I doing wrong? PLEASE help me!

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Can you show exactly how you are calling CheckCookie?

    Here's a useful line that you can save as a bookmark and execute when debugging cookie code:

    Code:
    javascript:alert("Cookies%20visible%20on%20current%20domain%20and%20path:\n\n"+document.cookie.split(/\;\s*/).join('\n\n'))

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles