View Full Version : Cookie Frustration
Muffin_LA
25-11-2005, 02:09/02:09AM
Hi everyone:
I am so stuck its not even funny. I have been working on this for over a week and just cant get it straight in my head.
I need to make a page where the user enters his/her name and email info. If they click the Register button right away the next time they come to the site the alert will display their name and email address.
If they do not Register, then a nag alert will pop up every 5 visits until they Register.
Right now the Register nag alert pops up every visit.
Any suggestions? Your help would be sooooooo greatly appreciated.
Linda
Code:
function setCookie(name, value, expires) {
var curCookie = name + "=" + encodeURI(value) +
((expires) ? "; expires=" + expires.toUTCString() : "");
document.cookie = curCookie;
}
function getCookie(name) {
var UserName;
var UserEmail;
var cook = document.cookie;
var bake = name + "=";
var crumbs = cook.indexOf("; " + bake);
if (crumbs == -1) {
crumbs = cook.indexOf(bake);
if (crumbs != 0) return null;
}
else
crumbs += 2;
var end = document.cookie.indexOf(";", crumbs);
if (end == -1)
end = cook.length;
return decodeURI(cook.substring(crumbs + bake.length, end));
UserName=document.nagger.user_name.value;
UserEmail=document.nagger.user_email.value;
document.cookie=bake+"="+UserName+"; expires=" + expires.toUTCString();
document.cookie=bake+"="+UserEmail+"; expires=" + expires.toUTCString();
}
function getUserInfo() {
var userInfo = decodeURI(document.cookie);
var info = userInfo.split("; ");
var userName = info[0].split("=");
var userEmail = info[1].split("=");
}
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>
<body>
<script type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
var now = new Date();
now.setFullYear(now.getFullYear() + 1);
var visits = getCookie("counter");
if (!visits) {
visits = 1;
window.alert("Welcome!!");}
else {
(visits == 5);
window.alert("Please register your information.");
}
setCookie("counter", visits, now);
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
<form name="nagger">
What is your name?<input type="text" name="user_name" /><br />
What is your email address? <input type="text" name="user_email" /><br />
<input type="submit" value="Register" />
</body>
</html>
donaldrennie
29-10-2006, 02:14/02:14AM
Hi Linda. I just ran across your "Cookee Frustration" post, and noticed I'm working on the same exact project. Mine's almost done. EVERYTHING works except it displays "User Name: Undefined Email: Undefined". Did you ever get yours worked out? I'll attach a txt document containing my work so far. Check it out. You'll see how I did the actual nag counter. That'll help you out. Maby you or somebody else out there can take a look at it and fix my little problem. I've got about 20 hours in this little puppy. Thanks.
Dave Hawley
29-10-2006, 03:35/03:35AM
Why on Earth would you want to annoy the Sh%$t out your site visitors.
Pop-up blockers were designed for site like this!
SEFL
29-10-2006, 04:00/04:00AM
Never mind the annoyance, there's a security issue (albeit a minor one) associated with this.
The cookie that is created would hold the user name and email (presumably). This cookie would thereby hold a piece of personally identifiable information in an unsecured format.
There's also the issue of doing this with Javascript, which some users don't have enabled (last I checked, the number was 13%). The much smarter way to do this would be to use a server-side programming language such as PHP or ASP, use the cookie to store a database unique identifier (e.g. an automatic increment counter) and pull the info from the database based on that.
If you do not know a server-side programming language, this would be a good time to learn.
Hawley: popup blockers wouldn't work in this instance, assuming I read this correctly. An alert box is merely an alert box, and not to be confused with a popup window (although you're right...they can be annoying.)
http://www.aspnetcenter.com/cliktoprogram/javascript/alert.asp
(Click the button about halfway down.)
Muffin_LA
29-10-2006, 04:25/04:25AM
Hi donaldrennie
No I never got it to work. I just finished the course without it.
Thanks SEFL I am going to check it out. This was part of a javascript course I had to do towards my certificate in web design.
Donald if you get it working please let me know and I will do the same if I get mine figured out. Would be nice to get it working so I have a copy of it just incase.
One day I would like to learn PHP but need to complete my certificate first.
Thanks again
Linda
Dave Hawley
29-10-2006, 05:28/05:28AM
Never mind the annoyance...popup blockers wouldn't work in this instance,Yeah, who needs human visitors :rolleyes:
Why oh why can't some sites let users decide without trying to nag them into something :confused: I bet you loose more than you nag into joining :cheers:
donaldrennie
29-10-2006, 09:38/09:38AM
Dave: It was a F^($'N HOMEWORK ASSIGNMENT!
Linda: We'll do. Thanks.
Connie
29-10-2006, 10:02/10:02AM
Hi donald and welcome. :hi:
Muffin_LA
29-10-2006, 11:31/11:31AM
Yes Dave its an assignment for a course we are taking. It's not something that I will ever use I am sure because I know how things like that can be annoying.
If you would have read the what Donald and I were saying you would have realized that. We wont be losing anything, actually we are gaining knowledge as to how things work and what javascript can do.
Good luck Donald. With cookies I need all the luck I can get.. lol
Thanks Linda
SEFL
29-10-2006, 13:15/01:15PM
Okay, as long as it's for a homework assignment (which I didn't realize myself), then that's "okay".
However, there are some comments I would like for you to print and take to your teacher, because he or she is leading you and your fellow classmates down the garden path. My advice to you would be to learn the syntax, but not the application. In other words, don't use the code you learn within the contexts which your teacher provides.
With that in mind, an open note to your teacher:
[list=1]
Web design isn't just about code. It's about building pages, scripts, and applications that are usable.
Nag screens and cookies that contain email addresses are not usable and pose security issues as well.
Teaching students who are still impressionable the concepts that you teach will lead to them focusing more on the concepts than on the syntax themselves in a number of cases, thereby spawning a series of unsuccessful web pages and sites. This creates issues both for students and for end users of those pages and sites.
If the problem of poor concepts is not of your own doing and a portion of a mandatory curriculum, then you would be well-served to forward these comments on to the appropriate individuals and insist that assignments such as these are designed with the end user in mind rather than simply the pursuit of academic achievement.
[/list=1]
donaldrennie
29-10-2006, 14:55/02:55PM
I just may do that, anonymously, even though he's not really 'teaching' these concepts. It wasn't even in the textbox. I think what Linda said is true. Also, I believe he was looking for something simple for us to research, and come up with on our own. Through the process, we learn stuff. It brought me to this particular forum, which, by the way, I like, so far. I sure did stir up a conversation in here, and so many people actually participating! I love being notified of replies by email, providing a link which takes me directly to the page where I need to go. I haven't seen that in other forums, yet. One thing that I'm confused about: I thanked Linda, (Muffin_LA), and 'Connie' said "Your Welcome". SEFL: You seem very knowledgable, and I hope I run into you again when I post other programming issues. Linda: Did you get your certificate? I hope somebody can spot what's wrong with the Javascript I submitted earlier. Thanks.
Connie
29-10-2006, 15:08/03:08PM
Donald you have misquoted me. 'Connie' said "Your Welcome"
What I said was
Hi Donald and welcome :hi: I was just welcoming you to the forum.
Muffin_LA
29-10-2006, 15:09/03:09PM
Thank you and I would send your comments on to the teacher but I am no longer in that course. Its been awhile since I first posted the question for help but I would still like to figure it out. Its just one of those things that nag at you until it finally clicks. I havent thought about it for awhile now, but since Donald is doing the same thing, I am interested in finding the end result.
Thanks again
Linda
Muffin_LA
29-10-2006, 15:14/03:14PM
Hi Donald
Not yet, I have to complete 2 web graphic courses but for the past 3 semesters there hasnt been enough people enrolled so they have been cancelled.. grrrrrrrrrr
I did pass the Javascript with a 72% which I thought was awesome because I figured i was going to fail.. lmao
I will keep checking to see if anyone can figure it out, and when I have a chance I am going to go back to mine and see what I can figure out also. They say if you step back and give it a rest it will come to you easier. Well I have stepped back so far that I probably wont remember any of it.. lol But I am going to give it a try.
Take care and keep in touch, fellow javascript student. lol
Linda
SEFL
29-10-2006, 17:39/05:39PM
I just got back from a hockey game and took a look at the code. Funny how a frozen brain can sometimes illuminate things.
The problem is here:
if (document.forms[0].user_name.value == ""
|| document.forms[0].user_email.value == "")
window.alert("Please enter a username and email.");
else
{
SetCookie("user_name", user_name, expdate, "/", null, false);
SetCookie("user_email", user_email, expdate, "/", null, false);
window.alert("You're now registered.");
}
There are two things wrong with this code. Since this is a homework assignment and I want to make sure you learn something from it, I won't tell you the answer. But I'll at least tell you the problems.
First off, you're not checking the email address field to see if anything's in it, or if it's valid.
Second, your code to write the cookies would be correct iiiiiiiiiiffffff you assigned the variables user_name and user_email values corresponding with the form fields. Right now, you haven't.
Remember: document.forms[0].user_email.value is not the same by default as user_email . It CAN be, with a line of code. But it's not by default.
Run with the ball for a bit and see how far you go. And if you get stuck, I'll try to help you as little as I can (normally it would be as much as I can, but I want you to find this yourself for your own sake.)
donaldrennie
29-10-2006, 18:09/06:09PM
SEFL: That makes sense to me. I'll get right on it. I should be able to fix that. Thank you very much! Your Great!
Connie: Thank you for welcoming me to the forum. You guys are great in here!
Linda: I may have it fixed soon, and I'll post a copy. Couldn't you take those other classes you need online with another college, transfer your credits, and get your certificate?
General Question: Is there a way of contacting an individual in this forum other than a reply?
Connie
29-10-2006, 18:25/06:25PM
General Question: Is there a way of contacting an individual in this forum other than a reply?
You should be able to send them a PM.
donaldrennie
29-10-2006, 18:42/06:42PM
SEFL: Wow! That was fast. You must have bells on your machine that lets you know when you get a message from here. I typed in the assignment statements, and WAMMO! It works! Man, the sight of it working correctly was like eye candy! Thanks again.
Connie: Thank you, and I'll see if I can figure out how to send a PM from in here.
Linda: It works now! Thanks to your initial posting and SEFL's help. I'm gonna clean is up a bit, code it so the nag counter is deleted when the user registers, and post it later. Thanks again.
I love this forum!
g1smd
29-10-2006, 19:01/07:01PM
>> I am so stuck its not even funny. I have been working on this for over a week and just cant get it straight in my head. <<
I spent more than 20 hours trying to get one simple .htaccess rule working last week ... only to find there was a typo in my test data ... aaarrgggghhhhh!
Dave Hawley
29-10-2006, 20:21/08:21PM
Dave: It was a F^($'N HOMEWORK ASSIGNMENT! Who's F^($'N talking to you? I was posting to Muffin_LA who said nothing about a "HOMEWORK ASSIGNMENT"
BTW, do you often get others to do your homework by hijacking forum threads of other members? You do not that this not how to learn.
If you would have read the what Donald and I were saying you would have realized thatYou said nothing about it in your post before I replied :rolleyes:
Donald you have misquoted me. 'Connie' said "Your Welcome" LOL! What were you saying about reading :D
Ok, carry on.
Muffin_LA
29-10-2006, 21:30/09:30PM
Sigh:
Well Dave you are right. I didnt say it was an assignment because I had been in here before for help and just assumed because I was the only one posting in this forum, people would know from my pass posts that it was an assignment. As for hijacking forum threads, I dont get others to do my assignment. I asked for help to try and understand this, and the teacher said it was fine to get help because sometimes another person can explain it in a different way that makes it click. All of my assignments have been my work, and I appreciate all the help I received. I guess I am just too dumb and old to figure it out on my own.
Thank you SEFL. I am going to go thru my coding tomorrow after work and see if I can get it working.
WTG Donald. I am glad you were able to get it working.
G1smd boy dont I know where you are coming from. I have done that a few times and sometimes its just impossible to see it. In the javascript course I was able to debug with no problems because it wasnt my work I was looking at.
Have a great one. And thank you again. Just remember Dave sometimes others need help to understand something.
Linda
Dave Hawley
29-10-2006, 21:35/09:35PM
You know what they say about Ass-u-me don't you? ;)
Linda, the hijack statement wasn't for you, which is why I qouted the hijacker.
SEFL
29-10-2006, 23:45/11:45PM
Originally posted by donaldrennie
SEFL: Wow! That was fast. You must have bells on your machine that lets you know when you get a message from here. I typed in the assignment statements, and WAMMO! It works! Man, the sight of it working correctly was like eye candy! Thanks again.
Connie: Thank you, and I'll see if I can figure out how to send a PM from in here.
Linda: It works now! Thanks to your initial posting and SEFL's help. I'm gonna clean is up a bit, code it so the nag counter is deleted when the user registers, and post it later. Thanks again.
I love this forum!
This might be the happiest a newcomer to the forums has been since I joined. :D
Glad I could help, dude. I'd like to see what you end up doing with it. Please repost.
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.