locked
Duplicate cookies RRS feed

  • Question

  • User1510859543 posted

    I have 2 pages that create or update cookies on my asp.net webforms app.  I added a way to display the key and values of all cookies in one of the pages and am finding some of them have doubled.  How can that happen as I thought if you set the value of a cookie that it would create if not there and update if already there.  I am setting them in code-behind as well as javascript. Below is what I use in javascript to set a cookie and also in code-behind to set a cookie value. What am I missing?

    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 ) {
      var today = new Date();
      today.setTime( today.getTime() );
      if ( expires ) {
        expires = expires * 1000 * 60 * 60 * 24;
      }
      var expires_date = new Date( today.getTime() + (expires) );
      document.cookie = name+"="+escape( value ) +
        ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
        ( ( path ) ? ";path=" + path : "" ) +
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ( ( secure ) ? ";secure" : "" );
    }
    
    
    Response.Cookies("x_amount").Value = Math.Round(dtr("ROCustomerTotal"), 2).ToString
    

    Tuesday, August 11, 2020 10:03 PM

Answers

  • User1535942433 posted

    Hi dlchase,

    Accroding to your description and codes,I create a test.I suggest you could add checkCookie function in javascript.

    Just like this:

    function checkCookie() {
      var user=getCookie("username");
      if (user != "") {
        alert("Welcome again " + user);
      } else {
         user = prompt("Please enter your name:","");
         if (user != "" && user != null) {
           setCookie("username", user, 30);
         }
      }
    }

    More details,you could refer to below article:

    https://www.w3schools.com/js/tryit.asp?filename=tryjs_cookie_username

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2020 2:28 AM
  • User-474980206 posted

    document.cookie returns a string of name/value pairs separated by a ; and space. the names need not be unique. the Response.Cookie api prevents duplication, but in fact dups are allowed.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2020 3:55 AM

All replies

  • User475983607 posted

    What am I missing?

    Debugging.  Your missing debugging.

    Wednesday, August 12, 2020 1:11 AM
  • User1535942433 posted

    Hi dlchase,

    Accroding to your description and codes,I create a test.I suggest you could add checkCookie function in javascript.

    Just like this:

    function checkCookie() {
      var user=getCookie("username");
      if (user != "") {
        alert("Welcome again " + user);
      } else {
         user = prompt("Please enter your name:","");
         if (user != "" && user != null) {
           setCookie("username", user, 30);
         }
      }
    }

    More details,you could refer to below article:

    https://www.w3schools.com/js/tryit.asp?filename=tryjs_cookie_username

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2020 2:28 AM
  • User-474980206 posted

    document.cookie returns a string of name/value pairs separated by a ; and space. the names need not be unique. the Response.Cookie api prevents duplication, but in fact dups are allowed.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2020 3:55 AM