Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookies

Status
Not open for further replies.

toughGirl

MIS
Mar 26, 2001
68
Please look at this!!

(
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Untitled</title>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--

function setCookie(n,v){
nextYear=new Date();
nextYear.setFullYear(nextYear.getFullYear()+1);
document.cookie=n+&quot;=&quot;+v+&quot;;expires=&quot;+nextYear.toGMTString();
}//end of function

function clearCookie(n){
var v=&quot;nothing&quot;;
lastYear=new Date();
lastYear.setFullYear(lastYear.getFullYear()-1);
document.cookie=n+&quot;=&quot;+v+&quot;;expires=&quot;+lastYear.toGMTString();
}//end of function

function getCookie(n){
theArray=document.cookie.split(&quot;; &quot;);
for(var i=0;i<theArray.length;i++){
if(theArray.split(&quot;=&quot;)[0]==n){
return theArray.split(&quot;=&quot;)[1];
}//end of if
}//end of for loop
return null;
}//end of function

function checkAll(f){
if(f.save_u.checked){
setCookie(&quot;username&quot;,f.username.value);
}
else{
clearCookie(&quot;username&quot;);
}
return true;

}
//-->
</SCRIPT>
</head>
<body>
<FORM ACTION=&quot;logIn_action.htm&quot; onSubmit=&quot;return checkAll(this)&quot; NAME=&quot;logIn_form&quot;>
username:
<INPUT TYPE=&quot;text&quot; NAME=&quot;username&quot;><BR>
password:
<INPUT TYPE=&quot;text&quot; NAME=&quot;password&quot;>
<BR>
save username?<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;save_u&quot;>
<INPUT TYPE=&quot;submit&quot;>
</FORM>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
if(getCookie(&quot;username&quot;)){
document.logIn_form.username.value=getCookie(&quot;username&quot;);
}
//-->
</SCRIPT>

</body>
</html>


I want to set both the username and password in a cookie. Currently, it is set only to making a username cookie. I also have a cookie_fns.js page.

Can anyone assist me?

tG
 
here's how I would do it.

<script language=JavaScript>
//Start of cookie data
expireDate = new Date
expireDate.setYear(expireDate.getYear()+1)

function setCookie(n , v){
var cookieStr = n + &quot;=&quot;+ v
document.cookie = cookieStr+&quot;;expires=&quot; + expireDate.toGMTString()
}

function getCookie(){
if(document.cookie.split('=')[0] == 'username'){
cookievalue = document.cookie.split(';')[0]
return cookievalue.split('=')[1]
}else{
return ''}
}

function clearCookie(n){
var v=&quot;nothing&quot;;
lastYear=new Date();
lastYear.setFullYear(lastYear.getFullYear()-1);
document.cookie=n+&quot;=&quot;+v+&quot;;expires=&quot;+lastYear.toGMTString();
}

function checkAll(){
if(document.logIn_form.save_u.checked){
setCookie(&quot;username&quot;, document.logIn_form.username.value);
setCookie(&quot;password&quot;, document.logIn_form.password.value);
}
else{
clearCookie(&quot;username&quot;);
clearCookie(&quot;password&quot;);
}
return true;
}
// End of cookie code
</script>
</head>
<body>
<FORM ACTION=&quot;logIn_action.html&quot; onSubmit=&quot;return checkAll(this)&quot; NAME=&quot;logIn_form&quot;>
username:
<INPUT TYPE=&quot;text&quot; NAME=&quot;username&quot;><BR>
password:
<INPUT TYPE=&quot;text&quot; NAME=&quot;password&quot;>
<BR>
save username?<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;save_u&quot;>
<INPUT TYPE=&quot;submit&quot;>
</FORM>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
if(getCookie(&quot;username&quot;)){
document.logIn_form.username.value=cookievalue.split(&quot;=&quot;)[1];
}
//-->
</SCRIPT>
</body>

hope this helps
paul
 
Did the script work for you? It doesn't work for me but I see 2 possible reasons why (just don't know how to rewrite these things):

1) in the getCookie function, it references only the 'username'

2)in the script below the form, it's also only referencing the 'username'

Perhaps this is ok, but the rest of the script is looking for username AND password cookies....

Also, perhaps I'm testing wrong. First, I enter a user name and password, check the box and press submit. As I progress to the next page, it notes my login name. I then press 'back', erase the username and password (keeping the checkbox checked) and press 'refresh'. The name and password don't come up again...?

so from this test is how I'm thinking it doesnt work.

Any insight?
tG
--{-{@
 
first if you want the cookie values to display when the page is loaded use

<body onLoad=ckCookie()>

so with that in mind here's what I changed.

<html>
<head>
<title>Untitled</title>
<script language=JavaScript>
//Start of cookie data

// Add from here-------------------------
function ckCookie(){
if(getCookie(&quot;username&quot;)){
document.logIn_form.username.value=cookievalue.split(&quot;=&quot;)[1];
}else{
document.logIn_form.username.value=&quot;&quot;;
}
if(getCookie(&quot;password&quot;)){
document.logIn_form.password.value=cookievalue.split(&quot;=&quot;)[1];
}else{
document.logIn_form.password.value=&quot;&quot;;
}
}

// to here ----------------------------------------

expireDate = new Date
expireDate.setYear(expireDate.getYear()+1)

function setCookie(n , v){
var cookieStr = n + &quot;=&quot;+ v
document.cookie = cookieStr+&quot;;expires=&quot; + expireDate.toGMTString()
}

function getCookie(){
if(document.cookie.split('=')[0] == 'username'){
cookievalue = document.cookie.split(';')[0]
return cookievalue.split('=')[1]
}else{
return ''}
}

function clearCookie(n){
var v=&quot;nothing&quot;;
lastYear=new Date();
lastYear.setFullYear(lastYear.getFullYear()-1);
document.cookie=n+&quot;=&quot;+v+&quot;;expires=&quot;+lastYear.toGMTString();
}

function checkAll(){
if(document.logIn_form.save_u.checked){
setCookie(&quot;username&quot;, document.logIn_form.username.value);
setCookie(&quot;password&quot;, document.logIn_form.password.value);
}
else{
clearCookie(&quot;username&quot;);
clearCookie(&quot;password&quot;);
}
return true;
}
// End of cookie code
</script>
</head>

<!-- Change Body tag -->
<body onLoad=ckCookie()>
<!-- --------------- -->

<FORM ACTION=&quot;logIn_action.html&quot; onSubmit=&quot;return checkAll(this)&quot; NAME=&quot;logIn_form&quot;>
username:
<INPUT TYPE=&quot;text&quot; NAME=&quot;username&quot;><BR>
password:
<INPUT TYPE=&quot;text&quot; NAME=&quot;password&quot;>
<BR>
save username?<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;save_u&quot;>
<INPUT TYPE=&quot;submit&quot;>
</FORM>
<!-- Here I removed all the javascript -->
</body>
</html>

Now when the page is reloaded it will display the username and the password IF the cookie exist.....

paul
 
ok, the script works good except the cookie isn't recordiing the password. it just inserts the username into both fields at refresh... tG
--{-{@
 
Ok I spent a little more time checking this time
I've removed the getCookie function and changed the
ckCookie function. here it is
//Start of cookie data

expireDate = new Date
expireDate.setYear(expireDate.getYear()+1)

function setCookie(n , v){
var cookieStr = n + &quot;=&quot;+ v
document.cookie = cookieStr+&quot;;expires=&quot; + expireDate.toGMTString()
}

function ckCookie(){
if(document.cookie) {// If cookie exist
cookievalue1 = document.cookie.split(&quot;;&quot;)[0];// Split cookies into name=value pairs and selects the first
document.logIn_form.username.value = cookievalue1.split(&quot;=&quot;)[1]// split name value and write to form
cookievalue2 = document.cookie.split(&quot;;&quot;)[1];// Split cookies into name=value pairs and selects the second
document.logIn_form.password.value = cookievalue2.split(&quot;=&quot;)[1]// split name value and write to form
}else{
document.logIn_form.password.value = &quot;&quot;
document.logIn_form.username.value = &quot;&quot;
}
}

function clearCookie(n){
var v=&quot;nothing&quot;;
lastYear=new Date();
lastYear.setFullYear(lastYear.getFullYear()-1);
document.cookie=n+&quot;=&quot;+v+&quot;;expires=&quot;+lastYear.toGMTString();
}

function checkAll(){
if(document.logIn_form.save_u.checked){
setCookie(&quot;username&quot;, document.logIn_form.username.value);
setCookie(&quot;password&quot;, document.logIn_form.password.value);
}
else{
clearCookie(&quot;username&quot;);
clearCookie(&quot;password&quot;);
}
return true;
}
// End of cookie code

I hope its all good this time.
ps. are you sure you want the password to be auto. displayed on load. It's not the most secure thing to do
as the cookie is stored on the computer and anyone can
access any computer. which means if Joe logs in and saves
the username and password then leaves and Mary uses Joe's computer Mary can login under Joe's username and password because it will be there as soon as the page loads...
?????????
paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top