Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Cookie help needed on user recognition

Posted on 2001-09-13
7
203 Views
Last Modified: 2013-12-18
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.
0
Comment
Question by:mfay72
  • 4
  • 3
7 Comments
 
LVL 24

Accepted Solution

by:
HemanthaKumar earned 100 total points
ID: 6479585
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
 

Author Comment

by:mfay72
ID: 6479761
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
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 6479976
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:mfay72
ID: 6479997
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
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 6480235
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
 

Author Comment

by:mfay72
ID: 6482270
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
 

Author Comment

by:mfay72
ID: 6487403
Thank you so much, Hemanth! This worked beautifully!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an old article, please see an updated version of this article, located here: http://www.experts-exchange.com/articles/23619/Notes-8-5x-Windows-7-Notes-info-and-tips.html
For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question