Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I add a unique number to each cookie?

    I'm trying to set a persistent cookie with a unique value when people visit my site through a certain landing page. I currently use

    Code:
    num_days = 180;
    function ged(noDays){
        var today = new Date();
        var expr = new Date(today.getTime() + noDays*24*60*60*1000);
        return  expr.toGMTString();
    }
    
    
    
    function setCookie(){
       
            document.cookie = "google=yes; path=/; expires=" + ged(num_days);
       
    }
    
    
    setCookie("google");
    I'd like each cookie to have a unique number added to it for tracking purposes...anyone who knows?

  • #2
    New Coder
    Join Date
    Apr 2007
    Location
    Silicon Valley California
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The timestamp mostly unique, unless you have substantial traffic, where it is possible for two people to arrive at the same time. If you require a pure JavaScript solution, then append a random number to the date.

    Code:
    var randomnumber=Math.floor(Math.random()*n)
    Where n is the maximum number you want plus one, so if I want values 0-100, then n would be 101.

    However, best practices would not have you create this identifier in JavaScript. It would be better to make a database on server called cookies (or something like that) with auto incrementation enabled on the primary key (PK). Then with an AJAX call, or simple server-side scripting, ask the database for the next available PK. This approach also allows you to use this Id to track the user's activity around your site and subsequent visits.

  • #3
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help, I'm still having a little bit of problem actually implementing it. Basically I want to add a random number to this

    Code:
    function setOptinCookie(cookieName){
    var start = document.cookie.indexOf(cookieName);
        if (start == -1){ 
           document.cookie = "optin=yes; path=/; expires=" + ged(num_days);
        } else {
            
        }
    }
    
    
    setOptinCookie("optin")


    I tried doing this:

    Code:
    function setCookie(){
       var randomnumber=Math.floor(Math.random()*1000000000000)
            document.cookie = "googleCheck=" + randomnumber ; "expires=" + ged(num_days);
       
    }
    
    
    setCookie("googleCheck");
    and it gives the cookie a random value, but the "expires=" part doesn't execute properly, so all I get is a session cookie, and I can't get the path to work.

    In the top code, how would I add the random number to the cookie without messing out the expiration of the cookie or the path it's set to?

    Thanks

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Not sure what you are doing with 'ged(num_days)', but this may help:
    Code:
    var theDate = new Date();
    theDate.setDate(theDate.getDate()+5) // 5 day ahead
    var num_days = theDate.toUTCString();
    
    function setOptinCookie(cookieName){
    var start = document.cookie.indexOf(cookieName);
        if (start == -1){
           var randomnumber=Math.floor(Math.random()*1000000000000);
           document.cookie = "optin=yes" + randomnumber + "; path=/; expires=" + num_days;
          }
         else {
    
          }
    setOptinCookie("optin");

  • #5
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, that did it


  •  

    Posting Permissions

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