...

View Full Version : Add document.cookie function to existing code



monkeyboy
01-21-2006, 10:44 AM
Hello. I've been trying (and failing) to add a document.cookie function to the code below...

How would I get it to write a cookie called "access" (of no particular value) to the user's computer before sending them to location.href?




<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Lefteris Haritou -->
<!-- Web Site: lef@writeme.com> www.geocities.com/~lef -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin

var base= new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z")

var pass=""

var z=23;

var y=28;

var f= new Array();

var K= new Array();

for (x=0; x<10; x++){

f[x]=x<<9

f[x]+=23

}

for (x=10; x<36; x++){

y=y<<1

v= Math.sqrt(y)

v = parseInt(v,16)

v+=5

f[x]=v

y++

}

for (x=36; x<62; x++){

z=z<<1

v= Math.sqrt(z)

v = parseInt(v,16)

v+=74

f[x]=v

z++

}

var iCounter = 1 //How many retries

function inc(){

iCounter--

if (iCounter > 0)

{

if (confirm("\nAfraid that wasn't it.\n\nGive it another bash?"))

Check()

else

alert('nice try sunshine');




// You may use this element istead if you want.

// location.href='denied.html' //Cancel html file



}

else

alert('nice try sunshine');

window.close();



// You may use this element instead if you want.

// location.href='denied.html' // 3 times incorrect html file



}

function Check(){

// If you want a javascript alert then use

// pass = prompt("please enter a password","")

// or for a textbox on the page use:

pass = document.getElementById('photos').value;

if(pass==null || pass==""){

}

else{

var lpass=(pass.length)+1

for (l=1; l<lpass; l++){

K[l]=pass.charAt(l)

}

var code=0;

for (y=1; y<lpass; y++){

for(x=0; x<62; x++){

if (K[y]==base[x]){

code+=f[x]

code*=y

}

}

}



<!-- STEP TWO: Put access code here! -->

if (code==79368) // code==[your access code]



go()

else

inc()

}

}

function go(){

location.href=pass+".htm";

}

// End -->

</SCRIPT>




Can anyone help? Thank you. :)

vwphillips
01-21-2006, 12:29 PM
function go(){
setCookie(); // the code to make or chamge the cookie
location.href=pass+".htm";

}


If you need help with the setCookie function ask

monkeyboy
01-21-2006, 01:31 PM
Thanks for your swift response. If you could point me in the right direction with the setCookie function that'd be great...I've been trying to do it by trial and error and failing!

Many thanks, Patrick.

vwphillips
01-21-2006, 02:08 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>

<body onload="zxcGetCookie('name');">
onunload is unreliable<br>
<script language="JavaScript" type="text/javascript">
<!--
var zxcDays=1; // The cookie will be available on revisits for a specified number of days


var zxcExp=new Date(new Date().getTime()+zxcDays*86400000).toGMTString();


function zxcGetCookie(name) {
var zxcst=document.cookie.indexOf(name+"=");
var zxclen=zxcst+name.length+1;
if ((!zxcst)&&(name != document.cookie.substring(0,name.length))) return null;
if (zxcst==-1) return null;
var zxcend=document.cookie.indexOf(";",zxclen);
if (zxcend==-1) zxcend=document.cookie.length;
return unescape(document.cookie.substring(zxclen,zxcend));
}

function zxcSetCookie(name,value){
document.cookie=name+"="+escape(value)+";expires="+zxcExp+";path=/;"
}

//-->
</script>
<script> vic=0; </script>
<form name=Show id=Show style="position:absolute;visibility:visible;top:50px;left:0px;" >
<input size=10 name=ShowX value="value"><input type="button" name="" value="SetCookie" onclick="zxcSetCookie('name',document.Show.ShowX.value);" ><br>
<input size=100 name=Show1 ><input type="button" name="" value="Show Cookie Value" onclick="document.Show.Show1.value=zxcGetCookie('name');" ><br>
</form>
</body>

</html>

monkeyboy
01-21-2006, 05:13 PM
Cheers for that... erm, I think I'm being a bit slow but am not sure about what to do with that code you posted! Think I'm a little out of my depth...

vwphillips
01-21-2006, 05:26 PM
function go(){
zxcSetCookie('name','value');
location.href=pass+".htm";
}

monkeyboy
01-21-2006, 10:08 PM
Right, this is the last time I pester you, I promise! I've tried various configurations of the text including adding all of the zxc variables etc but somehow it just won't work. At the moment, the whole page looks like this:




<html>
<head>
<title>this area is restricted</title>
<link rel="shortcut icon" href="../files/favicon.ico" type="image/x-icon" />
<link rel="icon" href="../files/favicon.ico" type="image/x-icon" />

<STYLE TYPE="text/css">
<!--
P A:link { text-decoration: none; color: darkblue; font-size: 12px; font-family: arial; line-height: 12px }
P A:active { text-decoration: none; color: darkblue; font-size: 12px; font-family: arial; line-height: 12px }
P A:visited { text-decoration: none; color: darkblue; font-size: 12px; font-family: arial; line-height: 12px }
P A:hover { text-decoration: underline; color: darkblue; font-size: 12px; font-family: arial; line-height: 12px }
P { font-size: 11px; font-family: arial; color: darkblue; line-height: 12px }
-->
</STYLE>


<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Lefteris Haritou -->
<!-- Web Site: lef@writeme.com> www.geocities.com/~lef -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin

var base= new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z")

var pass=""

var z=23;

var y=28;

var f= new Array();

var K= new Array();

for (x=0; x<10; x++){

f[x]=x<<9

f[x]+=23

}

for (x=10; x<36; x++){

y=y<<1

v= Math.sqrt(y)

v = parseInt(v,16)

v+=5

f[x]=v

y++

}

for (x=36; x<62; x++){

z=z<<1

v= Math.sqrt(z)

v = parseInt(v,16)

v+=74

f[x]=v

z++

}

var iCounter = 1 //How many retries

function inc(){

iCounter--

if (iCounter > 0)

{

if (confirm("\nAfraid that wasn't it.\n\nGive it another bash?"))

Check()

else

alert('nice try sunshine');




// You may use this element istead if you want.

// location.href='denied.html' //Cancel html file



}

else

alert('nice try sunshine');

window.close();



// You may use this element instead if you want.

// location.href='denied.html' // 3 times incorrect html file



}

function Check(){

// If you want a javascript alert then use

// pass = prompt("please enter a password","")

// or for a textbox on the page use:

pass = document.getElementById('photos').value;

if(pass==null || pass==""){

}

else{

var lpass=(pass.length)+1

for (l=1; l<lpass; l++){

K[l]=pass.charAt(l)

}

var code=0;

for (y=1; y<lpass; y++){

for(x=0; x<62; x++){

if (K[y]==base[x]){

code+=f[x]

code*=y

}

}

}



<!-- STEP TWO: Put access code here! -->

if (code==79368) // code==[your access code]



go()

else

inc()

}

}

function go(){

zxcSetCookie('name','value');

location.href=pass+".htm";

}

// End -->

</script>
</head>



<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td> <table background="../images/photback.jpg" align="center" valign="top" border=1 bordercolor="#333333" cellspacing=0 cellpadding=20>
<tr>
<td width="320" height="200" align="center" valign="middle"> <p>please enter a password to see the holiday snaps</p>
<FORM ACTION="#" NAME=verify>
<input id="photos" type=password size=10>
&nbsp;&nbsp;
<input type="button" value="go" onclick="javascript:Check()">
</FORM>
<SCRIPT TYPE="TEXT/JAVASCRIPT">
document.verify.photos.focus()
</SCRIPT> </td>
</tr>
</table></td>
</tr>
</table>


</body>
</html>



Quite simply I was hoping for users to enter a correct password, be given a cookie named "access" (to be checked for on a different page) and then be forwarded to the pass+".htm" location.

Sorry to bug you with this - will stick to simpler tasks in future (has been quite a learning experience though!)

vwphillips
01-21-2006, 11:02 PM
maybe tomorrow

monkeyboy
01-22-2006, 11:31 AM
Indeed, have bitten off more than I can chew. Any advice at all much appreciated at this stage. Especially any which reads "STEP AWAY FROM THE JAVASCRIPT AND PUT YOUR HANDS IN THE AIR!" :)

P.

vwphillips
01-22-2006, 12:17 PM
cook1.htm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>

<body >
<script language="JavaScript" type="text/javascript">
<!--
var zxcDays=1; // The cookie will be available on revisits for a specified number of days


var zxcExp=new Date(new Date().getTime()+zxcDays*86400000).toGMTString();


function zxcSetCookie(name,value){
document.cookie=name+"="+escape(value)+";expires="+zxcExp+";path=/;"
}

function Link(){
zxcSetCookie('access',document.Show.ShowX.value);
window.top.location='cook2.htm';
}

//-->
</script>
<script> vic=0; </script>
<form name=Show id=Show style="position:absolute;visibility:visible;top:50px;left:0px;" >
password = fred
<input size=10 name=ShowX value="value"><input type="button" name="" value="" onclick="Link();" ><br>
</form>
</body>

</html>

cook2.htm


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>

<body onload="zxcCookie();">
onunload is unreliable<br>
<script language="JavaScript" type="text/javascript">
<!--
var zxcDays=1; // The cookie will be available on revisits for a specified number of days


var zxcExp=new Date(new Date().getTime()+zxcDays*86400000).toGMTString();


function zxcGetCookie(name) {
var zxcst=document.cookie.indexOf(name+"=");
var zxclen=zxcst+name.length+1;
if ((!zxcst)&&(name != document.cookie.substring(0,name.length))) return null;
if (zxcst==-1) return null;
var zxcend=document.cookie.indexOf(";",zxclen);
if (zxcend==-1) zxcend=document.cookie.length;
return unescape(document.cookie.substring(zxclen,zxcend));
}

function zxcCookie(){
if (!zxcGetCookie('access')){
window.top.location='cook1.htm';
}
else if (zxcGetCookie('access')!='fred'){
window.top.location='cook1.htm';
return;
}
else {
alert('the cookie value is '+zxcGetCookie('access'));
}
}

//-->
</script>
<script> vic=0; </script>
<form name=Show id=Show style="position:absolute;visibility:visible;top:50px;left:0px;" >
<input size=100 name=Show1 ><br>
</form>
</body>

</html>

monkeyboy
01-22-2006, 01:22 PM
Brilliant, thanks.

Am done now!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum