General discussion

Locked

Write a cookie and the points are yours

By Adwait Gupte ·
I need a javascript cookie script that will write a cookie with the contents of a form and then when the user tries to login the script would compare the user name and the username on the cookie. There is no password. Also it will login the user andkeep a log of which links he has clicked and then make those links unavailable. The script should also keep track of how many links the user has clicked and read it each time he logs in and when the number reaches 200 should show a alert message. The number of links the user has clicked should be in a variable so that they can be shown on a page.

This conversation is currently closed to new comments.

8 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Write a cookie and the points are yours

by Jeremy In reply to Write a cookie and the po ...

I would do that using session variables. I would use VBScript - create a
database, and populate it with variables such as username, links, etc. Use
event handlers (ie when a user clicks on a link, have an 'onClick' event to
write to the db). Then, when the user logs in, you can immediately compare
his username (assuming you are using Windows NT) with that in the database,
and immediately access all the other info. You can then use the variables in
the database to control what the user sees.

Collapse -

Write a cookie and the points are yours

by Adwait Gupte In reply to Write a cookie and the po ...

No help via e mail though I mailed him.

Collapse -

Write a cookie and the points are yours

by mightyduk In reply to Write a cookie and the po ...

here's the html:
<html><head><title>Challenge</title>
<style>
A#disabled {color: gray;text-decoration: underline;}
A#done {font-color: blue;text-decoration: line-through;}
A#enabled {font-color: blue;text-decoration: underline;}
</style>
<script language="javascript" src="challenge.js"></script>
</head><body><h1>Challenge</h1>
<p>Enter name and click login to enter</p>
<form name="login_form" action="javascript:login();">
<input type="text" name="user">
<input type="submit" name="login" value="login">
<p><input type="text" name="numlinks" size=4></form>
<ol><li><a href="link1" id="disabled" onclick="return linkto(this);">first</a>
<li><a href="link2" id="disabled" onclick="return linkto(this);">second</a>
<li><a href="link3" id="disabled" onclick="return linkto(this);">third</a>
<li><a href="link4" id="disabled" onclick="return linkto(this);">first</a>
<li><a href="link5" id="disabled" onclick="return linkto(this);">second</a>
</ol></html>

Collapse -

Write a cookie and the points are yours

by Adwait Gupte In reply to Write a cookie and the po ...
Collapse -

Write a cookie and the points are yours

by mightyduk In reply to Write a cookie and the po ...

put the javascript in a file called challenge.js in the same directory:
function login(){
checkCookie();
if(document.login_form.user.value!=user){
alert("Login Failed!");
return;}

for(var i=0;i<document.anchors.length;i++){
if(linksbyname[document.anchors]){
document.anchors.id="done";
}
else {
document.anchors.id="enabled";
}
}
document.login_form.numlinks.value=numlinks;
if(numlinks>=200){
alert("Congratulations you hit 200!");}
}

function checkCookie(){
if(document.cookie){
eval(document.cookie);
var linklist=links.split(",");
for(var i=1;i<linklist.length;i++){
linksbyname[linklist]=true;}
}
else{
user=document.login_form.user.value;
document.cookie="user='"+user+"'"+expiry;
document.cookie="numlinks=0"+expiry;
document.cookie="links='none'"+expiry;
}
}

Collapse -

Write a cookie and the points are yours

by Adwait Gupte In reply to Write a cookie and the po ...
Collapse -

Write a cookie and the points are yours

by mightyduk In reply to Write a cookie and the po ...

and finally:

function linkto(target){
if(linksbyname[target.href]){
document.login_form.user.select();
document.login_form.user.focus();
return false;
}
else {
linksbyname[target.href]=true;
target.id="done";
numlinks++; document.login_form.numlinks.value=numlinks;
links+=","+target.href;
document.cookie="links='"+links+"'"+expiry;
document.cookie="numlinks="+numlinks+expiry
return true;
}
}

var numlinks=0;
var links="";
var expiry=";expires=Fri, 31-Dec-2099 12:00:00 GMT";
var user="";
var linksbyname=new Array();

the cookie will expire at the end of the century, should be long enough? It runs with IE5, you might have to play with it a little for Netscape. Email me if you have questions

Collapse -

Write a cookie and the points are yours

by Adwait Gupte In reply to Write a cookie and the po ...
Back to Web Development Forum
8 total posts (Page 1 of 1)  

Related Discussions

Related Forums