Cookie help needed on user recognition

Could someone please help me! I have a discussion forum application, and my customer would like for users to be "remembered" when the revisit the site after registering and  logging in for the first time.

I can't really manipulate the Custom Login form because this is form that is used for all our applications on that server. So I am trying to write a cookie or something that will do the task.
What happens now is when a user registers, after they get confirmation of the username and password they chose, they are redirected to the $$ViewTemplateDefault form, and I have the &Login parameter attached. This way, they are prompted to login right away. This is the form they also come back to when visiting the site subsequently, but they aren't prompted to login on a revisit. I would like to set it up so that when a user comes back to the $$ViewTemplateDefault form, a message displays saying "Welcome Back" and their username. But I still want them to login in order to post a message or  respond to one. I just want the cookie to store the username. Any ideas on how I would do that with a JavaScript cookie? Or is there a Notes way to do this?

Any help would be greatly appreciated.
mfay72Asked:
Who is Participating?
 
HemanthaKumarCommented:
Use javascript routine below to set and get cookie values

Call setCookie like this via javascript, you will send the value of the fields to this function called initCookie with cookie identifier

In initCookie,
cookiename would be the identifier for the cookie
paravalue would be the actual value
now would define the expiry of cookie


=======================
function initCookie(cookieName, paraValue) {
        var now = new Date();
     // cookie expires in one day
     now.setTime(now.getTime() + 1 * 24 * 60 * 60 * 1000);
     // set the new cookie
     setCookie(cookieName, paraValue, now);
}

function setCookie(name, value, expires, path, domain, secure) {
     var curCookie = name + "=" + value +
         ((expires) ? "; expires=" + expires.toGMTString() : "") +
         ((path) ? "; path=" + path : "") +
         ((domain) ? "; domain=" + domain : "") +
         ((secure) ? "; secure" : "");
     document.cookie = curCookie;
}

function getCookie(name) {
     var dc = document.cookie;
     var prefix = name + "=";
     var begin = dc.indexOf("; " + prefix);
     if (begin == -1) {
       begin = dc.indexOf(prefix);
       if (begin != 0) return null;
     } else
       begin += 2;
     var end = document.cookie.indexOf(";", begin);
     if (end == -1)
       end = dc.length;
     return dc.substring(begin + prefix.length, end);
}


~Hemanth
0
 
mfay72Author Commented:
Hemanth,

Thank you so much for answering... I think this is exactly what I'm looking for. Just one question: How would I declare paraValue? For example, I would say

function initCookie(userName, paraValue) and you said that paraValue would be the actual value....or do I say function initCookie(cookieName, userName)? I'm confused as to what the actual value actually is.

I'm sorry for my lack of expertise in this...I'm familiar with JavaScript, but new to cookies.

Melanie
0
 
HemanthaKumarCommented:
for eg: if you have a field called user in the login form, then call the function like this.

initCookie( "UserName", document.forms[0].user.value)

Here UserName would be the cookie name and user.value will be cookie value
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
mfay72Author Commented:
OK...I think I see....would I call this in the onload event of the form then? Also, would I need to declare var curCookie like this:

var curCookie = name + "=" + document.forms[0].user.value

Melanie
0
 
HemanthaKumarCommented:
No, don't alter the setcookie and getcookie methods, they will just set or read cookie from local machine.

OnUnload event of the form call this function like initCookie( "UserName", document.forms[0].user.value)

which will create username cookie in local machine

In onload event use getCookie to determine if there is a cookie named "UserName" if exists then extract value of the cookie and welcome the user.

Hope it is clear.

0
 
mfay72Author Commented:
OK...I will try this today, and let you know how it comes out. I got very busy yesterday afternoon, which is why I didn't respond sooner. Thank you again, Hemanth, for all your help in this matter...you're the best!
0
 
mfay72Author Commented:
Thank you so much, Hemanth! This worked beautifully!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.