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 12 of 12
  1. #1
    Regular Coder
    Join Date
    May 2003
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Creating cookie to remember a user preference

    I am needing to create a cookie that will be created after a user fills out a form. Depending on the users zip code, they would get one of 5 different tour guides for our site. This number value should be stored in a cookie, and when the user browses the site, that number (which coorisponds to a tour guide) will stay with them.

    I found some code, and can get a basic cookie working, but I get lost when getting more advanced.

    Code:
    <html>
    <head>
    <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.length
        return unescape(document.cookie.substring(c_start,c_end))
        } 
      }
    return ""
    }
    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())
    }
    function checkCookie()
    {
    username=getCookie('username')
    if (username!=null && username!="")
      {alert('Welcome again '+username+'!')}
    else 
      {
      username=prompt('Please enter your name:',"")
      if (username!=null && username!="")
        {
        setCookie('username',username,365)
        }
      }
    }
    </script>
    </head>
    <body onLoad="checkCookie()">
    </body>
    </html>

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Was there a reason to use a cookie instead of a session variable for this type of thing?

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    Regular Coder
    Join Date
    May 2003
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Was there a reason to use a cookie instead of a session variable for this type of thing?
    I guess I am not super familiar with this, so that is why I thought cookies would be the best way to go. I just need the user to be able to browse the site, and have their "tour guide" stay with them on specific pages.

    Could this be done with session variables?

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Yes, and it should be, really. Anything "per visit" should use sessions or other state bags (.net) if there's not too much data. If there is a lot of data, cookies and sessions will need additional things to help them, but that's not your issue here.
    Now, the session id itself may be a cookie, or it may be passed via the URL. But limit your cookie use to things you want saved over time, such as a "remember me" login type thing. You are limited to how many cookies you can store as well as the amount of data in them, plus most languages make it very easy to use the session state. Using cookies for everything was popular before using session was easy, or even possible. (do beware that TOO much in the session can kill the server, but that much can't use cookies anyway)

    What server-side language are you using, asp.net, php...?
    I can see using a cookie only if you had no server-side scripting, but since I see a "username" I assume there's logging in and whatnot and you have some server-side tech going on there.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    Regular Coder
    Join Date
    May 2003
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    What server-side language are you using, asp.net, php...?
    I can see using a cookie only if you had no server-side scripting, but since I see a "username" I assume there's logging in and whatnot and you have some server-side tech going on there.
    Well, we are running a CMS which uses php. But, we cannot paste php code into the CMS for security reasons, I would have to build a custom module for that. This is why I was turning to javascript for this.

    There was to be no logging in, I was going to have the user enter their zip code in, and depending on their zip code, they would be assigned a tour guide for the site, which they would see on various pages as their guide.

    I do think it would be nice to still use a cookie so that when they visit our site again they have the same tour guide that remembers them, rather than just that one session.

  • #6
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Ah, I see. Then yes, a cookie would be fine here.
    I see the basic cookie functions, writing it and reading it, are here -- do they work as desired?
    If so, where do you start to have difficulty? Getting the zipcode into the cookie, or ...? Where does the zipcode come from? Unless it is the result of a GET, you can't get it with pure client-side javascript. You'd need at least a little server-side code to get the POST.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #7
    Regular Coder
    Join Date
    May 2003
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah, I see. Then yes, a cookie would be fine here.
    I see the basic cookie functions, writing it and reading it, are here -- do they work as desired?
    If so, where do you start to have difficulty? Getting the zipcode into the cookie, or ...? Where does the zipcode come from? Unless it is the result of a GET, you can't get it with pure client-side javascript. You'd need at least a little server-side code to get the POST.
    The zip code would be entered into a form, which then could use GET to put the zip code into the URL, then hold that in the cookie to be used in later pages where I can say, if this zip code, then this person is your guide. I guess I am just confused on how to pull the information out of the cookie and have that interact with the server and pull information that is appropriate.

  • #8
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    pull the information out of the cookie and have that interact with the server and pull information that is appropriate
    Well, that would highly depend on the back end, really. That's well beyond the javascript portion. That's why I figured you had control of the back end.
    You'd have to really elaborate on what's going on that you're trying to modify for me to be able to help, and if you can't modify the php source, this may become a hack of url rewriting or some such to "fool" the CMS.

    Honestly, I wouldn't even try this sort if thing with a CMS that I didn't have the source to in order to modify it to support it, if it were not already a passable parameter via the URL or cookie.

    Depending on the users zip code, they would get one of 5 different tour guides for our site. This number value should be stored in a cookie, and when the user browses the site, that number (which coorisponds to a tour guide) will stay with them
    See, that's do-able with javascript and cookies. But getting the back end to CARE about that value that sticks around with the user, that's the server-side.
    How does the server-side currently decide on the tour guide? That's where you need to start looking.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #9
    Regular Coder
    Join Date
    May 2003
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well, that would highly depend on the back end, really. That's well beyond the javascript portion. That's why I figured you had control of the back end.
    You'd have to really elaborate on what's going on that you're trying to modify for me to be able to help, and if you can't modify the php source, this may become a hack of url rewriting or some such to "fool" the CMS.

    Honestly, I wouldn't even try this sort if thing with a CMS that I didn't have the source to in order to modify it to support it, if it were not already a passable parameter via the URL or cookie.
    I could modify the php source, as the CMS is open source (MySource Matrix) but I really do not want to get into that.

    I guess I should first ask the question, once a value is saved in the cookie, how can I pull that out and pass that to the URL?

  • #10
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    For a link, you can do this. It should function with and without script/cookies, and should work for users like me who are addicted to middle click to open in a new window.
    It will NOT work if the user chooses Open In New Window (IE).

    test.html
    Code:
    <html>
    <head>
    <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.length
        return unescape(document.cookie.substring(c_start,c_end))
        } 
      }
    return ""
    }
    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())
    }
    function checkCookie()
    {
    zip=getCookie('zip')
    if (zip==null || zip=="") 
      {
      zip=prompt('Please enter your zip',"")
      setCookie('zip',zip,365)
      }
    }
    </script>
    </head>
    <body onLoad="checkCookie()">
    <a href="test2.html" onClick="this.href+='?zip='+getCookie('zip')">Clicky</a>
    </body>
    </html>

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #11
    Regular Coder
    Join Date
    May 2003
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Awesome! What do I do if I do not want to edit the body tag of the page, since with the CMS we work with a template system. Is there a way to load it without body onLoad?

  • #12
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Sure, just put the call in anywhere prior to its first use.

    Code:
    <body>
    <script type="text/javascript">
    checkCookie();
    </script>
    <a href="test2.html" onClick="this.href+='?zip2='+getCookie('zip2')">Clicky</a>
    </body>
    </html>

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

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