Solved

Changing cookie expiration

Posted on 2011-02-17
12
562 Views
Last Modified: 2012-05-11
I am running a script that currently sets a cookie that expires at the end of the session when the browser close's.  I would like to set the expiration date for 360 days.  Not sure how to make this change in the following code:

      setCookie:function(name, value, days){
         var expirestr=''
         if (typeof days!="undefined") //if set persistent cookie
            expirestr="; expires="+expireDate.setDate(new Date().getDate()+days).toGMTString()
        document.cookie = name+"="+value+"; path=/"+expirestr
      }
0
Comment
Question by:TCCIRM
  • 7
  • 4
12 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34915532
get the old value first (getCookie(c_name)) and recreate (setCookie:function(name, value, days)) it with the new expiration date :)
0
 
LVL 3

Accepted Solution

by:
imaki06 earned 250 total points
ID: 34915540
You should find the call to that setCookie-function.

There is three parameters name, value and amount of days. Change the last parameter to 360, and thats it..

Like this:
setCookie("name","cookie value", 360);
0
 

Author Comment

by:TCCIRM
ID: 34915584
So modification to this string will not do the trick?

expirestr="; expires="+expireDate.setDate(new Date().getDate()+days).toGMTString()

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 34915606
setCookie(myCookieName, getCookie(myCookieName), 360);

Open in new window

0
 

Author Comment

by:TCCIRM
ID: 34915727
Okay I'm following you somewhat...  I'm deconstucting this to learn.  I think I should have included the entire routine:  This is not as clear cut as I thought it was.  The cookies name is fluidtextresizer.   So by calling the old value (if it is available) first...  then set the expiration date?

fluidtextresizer.routines={
      getCookie:function(Name){
      var re=new RegExp(Name+"=[^;]*", "i");   //construct RE to search for target name/value pair
      return (document.cookie.match(re))? document.cookie.match(re)[0].split("=")[1] : null //return cookie value if found or null
      },
      
      setCookie:function(name, value, days){  
      var expirestr=''
      if (typeof days!="undefined") //if set persistent cookie
      expirestr="; expires="+expireDate.setDate(new Date().getDate()+days).toGMTString()  
      document.cookie = name+"="+value+"; path=/"+expirestr
      }
}
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34915740
>The cookies name is fluidtextresizer.   So by calling the old value (if it is available) first...  then set the expiration date?

yes, if it not exist you're just creating a cookie with a null value
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:TCCIRM
ID: 34916268
Yeah I'm not getting it...   Can't seem to adapt your recommendations into the code I have and get it to work....  The principle makes sense, application does not...
0
 

Author Comment

by:TCCIRM
ID: 34916300
I guess the crazy part is I can get the simple application to work but this script is a little more complicated.  Complete script attached... fluidtextresizer.js
0
 

Author Comment

by:TCCIRM
ID: 34917355
Still plugging away...  still not able to make the leap...  
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34935404
If you place alert/console.log as first command of setCookie and getCookie do you get/see them ?
0
 

Author Comment

by:TCCIRM
ID: 34950914
As you tell by now, I'm not real adept with JavaScript...  I tried the following but it throws and error:

alert( document.cookie("fluidtextresizer") );

Using FF cookie viewer I am able to view the values and can see whether my modifications are working.  I've been able to create a simple cookie separate from the script I attached. I am forced to use this script but have not been able to make the leap....
0
 

Author Closing Comment

by:TCCIRM
ID: 34980277
Answers were not specfic to my problem.  Moving on...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now