Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-21-2006, 10:44 AM   PM User | #1
monkeyboy
New Coder

 
Join Date: Jan 2006
Location: Nottingham, UK
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
monkeyboy is an unknown quantity at this point
Add document.cookie function to existing code

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?

Code:
<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.
monkeyboy is offline   Reply With Quote
Old 01-21-2006, 12:29 PM   PM User | #2
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,382
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
function go(){
setCookie(); // the code to make or chamge the cookie
location.href=pass+".htm";

}
If you need help with the setCookie function ask
vwphillips is offline   Reply With Quote
Old 01-21-2006, 01:31 PM   PM User | #3
monkeyboy
New Coder

 
Join Date: Jan 2006
Location: Nottingham, UK
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
monkeyboy is an unknown quantity at this point
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.
monkeyboy is offline   Reply With Quote
Old 01-21-2006, 02:08 PM   PM User | #4
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,382
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<!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>
vwphillips is offline   Reply With Quote
Old 01-21-2006, 05:13 PM   PM User | #5
monkeyboy
New Coder

 
Join Date: Jan 2006
Location: Nottingham, UK
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
monkeyboy is an unknown quantity at this point
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...
__________________
Perfectionism and incompetence... the perfect combination
monkeyboy is offline   Reply With Quote
Old 01-21-2006, 05:26 PM   PM User | #6
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,382
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
function go(){
 zxcSetCookie('name','value');
 location.href=pass+".htm";
}
vwphillips is offline   Reply With Quote
Old 01-21-2006, 10:08 PM   PM User | #7
monkeyboy
New Coder

 
Join Date: Jan 2006
Location: Nottingham, UK
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
monkeyboy is an unknown quantity at this point
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:

Code:
<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!)
__________________
Perfectionism and incompetence... the perfect combination
monkeyboy is offline   Reply With Quote
Old 01-21-2006, 11:02 PM   PM User | #8
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,382
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
maybe tomorrow
vwphillips is offline   Reply With Quote
Old 01-22-2006, 11:31 AM   PM User | #9
monkeyboy
New Coder

 
Join Date: Jan 2006
Location: Nottingham, UK
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
monkeyboy is an unknown quantity at this point
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.
monkeyboy is offline   Reply With Quote
Old 01-22-2006, 12:17 PM   PM User | #10
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,382
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
cook1.htm
Code:
<!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

Code:
<!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>
vwphillips is offline   Reply With Quote
Old 01-22-2006, 01:22 PM   PM User | #11
monkeyboy
New Coder

 
Join Date: Jan 2006
Location: Nottingham, UK
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
monkeyboy is an unknown quantity at this point
Brilliant, thanks.

Am done now!
monkeyboy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:57 PM.


Advertisement
Log in to turn off these ads.