Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3665672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:47:21+00:00 2026-05-19T01:47:21+00:00

I am just learning JS cookies. I am able to save the cookie when

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:47:21+00:00Added an answer on May 19, 2026 at 1:47 am

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just learning about SproutCore now, seems great. But I can't find a good
Just learning about Dependency Injection and Prism... It seems just asking around alot of
Im just learning the ropes with javascript and jQuery but cant get my head
I'm just learning python and I'm not sure exactly what's supposed to go in
I'm just learning java out of a what seems to be an excellent book,
Just learning php and looking into someone else's code. I'm not sure what is
I am just learning JavaScript at the moment and I have been able to
I'm only just learning javascript so I imagine this is relatively simple but it's
Just learning C++, encountered an issue that i'm not really sure why the following
just learning Java and I know this may sound stupid but I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.