Zach Smith
Zach Smith

Reputation: 5684

JavaScript Cookie Length

I am just learning JS cookies. I am able to save the cookie when a person hits a button, but seems the length of period is not correct. I need it to be 15 days, but it keeps saying the length of time is until the end of the session (http://screencast.com/t/ea7TOoGTnbA).

Here is the code I am using below:

<script type="text/javascript">
  function getCookie(w){
    cName = "";
    pCOOKIES = new Array();
    pCOOKIES = document.cookie.split('; ');
    for(bb = 0; bb < pCOOKIES.length; bb++){
        NmeVal  = new Array();
        NmeVal  = pCOOKIES[bb].split('=');
        if(NmeVal[0] == w){
            cName = unescape(NmeVal[1]);
        }
    }
    return cName;
}
function printCookies(w){
    cStr = "";
    pCOOKIES = new Array();
    pCOOKIES = document.cookie.split('; ');
    for(bb = 0; bb < pCOOKIES.length; bb++){
        NmeVal  = new Array();
        NmeVal  = pCOOKIES[bb].split('=');
        if(NmeVal[0]){
            cStr += NmeVal[0] + '=' + unescape(NmeVal[1]) + '; ';
        }
    }
    return cStr;
}
function setCookie(name, value, expires, path, domain, secure){
    document.cookie = name + "=" + escape(value) + "; ";

    if(expires){
        expires = setExpiration(expires);
        document.cookie += "expires=" + expires + "; ";
    }
    if(path){
        document.cookie += "path=" + path + "; ";
    }
    if(domain){
        document.cookie += "domain=" + domain + "; ";
    }
    if(secure){
        document.cookie += "secure; ";
    }
}

function setExpiration(cookieLife){
    var today = new Date();
    var expr = new Date(today.getTime() + cookieLife  * 60 * 60 * 10);
    return  expr.toGMTString();
}

</script>

<script language="JavaScript">
// set a cookie which will expire in 3 days and be accessible site wide
setCookie('drunkdriving_cta_overlay', 'Yes', 3, '/');
</script>


<script language="JavaScript"><!--
document.write(getCookie("drunkdriving_cta_overlay"));
//-->
</script> 

<?php if (isset($_COOKIE["drunkdriving_cta_overlay"])) {?>
hello?
<?php } else { ?>
goodby
<?php } ?>

Any help would be greatly appreciated.

Upvotes: 0

Views: 4814

Answers (1)

Jeremy Vanderburg
Jeremy Vanderburg

Reputation: 842

It looks like the problem is not in setting the cookie, but in the date calculation. Try replacing your setExpiration method with this:

function setExpiration(cookieLife){ 
    var expires = new Date();
    expires.setDate(expires.getDate()+cookieLife);
    return expires.toGMTString();
}

The date calculation in your method does not seem to work correctly.

Edit:

Try this code instead. I think I found the issue.

<script type="text/javascript">
  function getCookie(w){
    cName = "";
    pCOOKIES = new Array();
    pCOOKIES = document.cookie.split(';');
    for(bb = 0; bb < pCOOKIES.length; bb++){
        NmeVal  = new Array();
        NmeVal  = pCOOKIES[bb].split('=');
        if(NmeVal[0] == w){
            cName = unescape(NmeVal[1]);
        }
    }
    return cName;
}
function printCookies(w){
    cStr = "";
    pCOOKIES = new Array();
    pCOOKIES = document.cookie.split(';');
    for(bb = 0; bb < pCOOKIES.length; bb++){
        NmeVal  = new Array();
        NmeVal  = pCOOKIES[bb].split('=');
        if(NmeVal[0]){
            cStr += NmeVal[0] + '=' + unescape(NmeVal[1]) + ';';
        }
    }
    return cStr;
}
function setCookie(name, value, expires, path, domain, secure){
    var cookie = name + "=" + escape(value);

    if(expires){
        expires = setExpiration(expires);
        cookie += ";expires=" + expires;
    }
    if(path){
        cookie += ";path=" + path;
    }
    if(domain){
        cookie += ";domain=" + domain;
    }
    if(secure){
        cookie += ";secure";
    }
    document.cookie = cookie;
}

function setExpiration(cookieLife){
    var expires = new Date();
    expires.setDate(expires.getDate()+cookieLife);
    return expires.toGMTString();
}

</script>

<script language="JavaScript">
// set a cookie which will expire in 3 days and be accessible site wide
setCookie('drunkdriving_cta_overlay', 'Yes', 3, '/');
</script>


<script language="JavaScript"><!--
document.write(getCookie("drunkdriving_cta_overlay"));
//-->
</script>

Upvotes: 1

Related Questions