Help - Search - Members - Calendar
Full Version: Cookies!
Zymic Webmaster Forums > Web Design & Development > Client Side Scripting > JavaScript
trut2
Hello,

I have a somewhat serious issue, my cookie scripts don't work. If they dont work, my entire webpage is useless, i have included all source codes and even a link to mysite, Any and All help is appreciated.

MAIN PROGRAM:
(cookie.html)
CODE
<html>
<head>
<meta http-equiv="refresh" content="5">
&lt;script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return ""
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}

function checkCookie()
{

var username=getCookie('username');
var password=getCookie('password');
var now = new Date()
var today = now.getDate()
var days = today-expire;
if (username!=null && username!="")
  {
    var user=prompt("Enter Your Username: ");
    var pass=prompt("Enter Your Password: ");
    if(user == username)
    {
        if(pass == password)
        {
                document.write("<font color='#A4A4A4' size='5'>Welcome "+username);
        }
        else
        {
            document.write("Error Found, Reset in 5 Seconds");
        }
    }
    else
    {
        document.write("Error Found, Reset in 5 Seconds");
    }
  }
else
  {

var username=prompt("Welcome, please enter your username:");
  if (username!=null && username!="")
    {
    var exp = 5;
    setCookie('username',username,exp);
var password=prompt("Please enter your password:");
var password2=prompt("Confirm Password:");
if(password == password2)
{
    setCookie('password',password,exp);    
    document.write("In a moment you will have to re-enter your data");
}
else
{
    document.write("Error In Password Set");
}
    }
  }
}
</script>
</head>
<body onLoad="checkCookie()">

</body>
</html>

REMOVE COOKIE
(cookie_eat.html)
CODE
<html>
<head>
&lt;script type="text/javascript">
function eat_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
function ohno(){
var r=(Math.floor(Math.random()*11));
var cookie = "";
if(r>5){
cookie="Cancer Cookie";
}
else{
cookie="Peanut Butter Cookie";
}
    var r=confirm("Are you sure you want to eat the "+cookie+"?");
    if (r==true)
        {
            document.write("Noming Cookies...");
            eat_cookie('username');
            eat_cookie('exp');
            document.write("<br>Cookies Eaten!");
            document.write("<br><a href='cookie.html'>Click here to go home</a>");
        }
    else
        {
            donothing();
    }
}
function donothing(){
    document.write("It is safe to redirect from this page, your cookie is saved<br>");
}
</script>
<title>
Eat Cookie
</title>
</head>
<body>
<input type='button' id='eat' name='eat' value='Eat Cookie' onClick='java script:ohno()'>
<input type='button' id='ew' name='ew' value='Leave Cookie' onClick='java script:donothing()'>
</body>
</html>

LINK
(click here to go to my site)

cookie_eat.html works just fine, i just posted it in case someone needed that script for whatever reason.
If you need any additional data, please let me know. This same script (cookie.html) is the EXACT same script that controls all of my Login forms, and my register form is in the process of being reverted back to a standard form

Thank You in advance for any help what so ever.

And sorry if i just wasted an entire thread because a spelling error, i could not find one thing wrong with it..

Sincerely,

Turt2
(or trut2, whichever)

((Some source code is from w3 schools ))
trut2
*sigh** after a little scowering of the JavaScript forums, i thank Ed for mentioning firebug, it helped, ALOT, Thank you

((i had one too many variables))
EvilMastermindsProgram
I can't seem to open your site link...

So I can't check the error out......
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.