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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting a cookie 1

Status
Not open for further replies.

wouter

Technical User
Jul 3, 2000
45
0
0
NL
Hello,

ik want to set a cookie and store the userid in it. The cookie should expire after 30 minutes. Can i do this. I'm reading the Cold Fusion web application construction kit, but it says how to set the cookie to expire at a given date or after a few days.

<cfcookie name=&quot;info&quot; value=&quot;#user_id&quot; expires=&quot;30&quot;>
I think this cookie expires after 30 days. I don't know how to do it right.

Wouter

Wouter
zure_zult@hotmail.com
To me, boxing is like a ballet, except there's no music, no choreography, and the dancers hit each other.
 
Hi,

Is it totally necessary to use a cookie? You can set a session variable and expire that in 30 minutes by use of the application.cfm file.

in your template
<cfset session.id = 1234>

in your application.cfm file
<CFAPPLICATION NAME=&quot;MediaPost&quot;
CLIENTMANAGEMENT=&quot;Yes&quot;
SESSIONMANAGEMENT=&quot;Yes&quot;
SESSIONTIMEOUT=#CreateTimeSpan(0,0,30,0)#>

Kecko Technologies
 
Hey Wouter,

I'm not sure that you can make a cookie expire in a shorter period. Based on my docs, it appears that the cookie contains a date on which it expires. You could try setting it to be a date/time value but you might find that certain browsers ignore the time part if that even works.

It sounds like you really want to use session variables if you need something to expire within 30 minutes. You could create an application with <cfapplication> and set the timeout to 30 minutes. You could then use a session variable to store any information you were storing in cookies. Something like this might be what you could use:

<cfapplication
name=&quot;myApp&quot;
sessionmanagement=&quot;yes&quot;
sessiontimeout=#createtimespan(0,0,30,0)#
>
<cfparam name=&quot;session.loggedIn&quot; default=&quot;no&quot;>
<cfif session.loggedIn neq &quot;yes&quot;>
<cflocation url=&quot;login.cfm&quot;>
</cfif>

Hope this helps,
GJ
 
hi wouter

A method exists for cookies, but it is slightly more complex. For storing multiple values describing a user in one cookie, you can set the cookie = a list. The list could have 3 elements.

#userID#,#username#,#dateadd(&quot;s&quot;, 1800, Now())#

If listGetAt(cookie.info,2) is the username, then listGetAt(cookie.info,3) is the timestamp of when the cookie will expire. To check on every page, use:


<cfset temp.timestamp = listgetat(decrypt(cookie.info, key), 3)>

<cfif temp.timestamp GTE Now() AND temp.timestamp LTE dateadd(&quot;s&quot;, 1800, Now())>
<!--- encrypt string --->
<cfcookie name=&quot;info&quot; value=&quot;#encrypt(&quot;#userid#,#username#,#temp.timestamp#&quot;, key)#&quot;>
<cfelse>
<cfcookie name=&quot;info&quot; value=&quot;0&quot; expires=&quot;NOW&quot;>
<cflocation url=&quot;login.cfm&quot;>

</cfif>


You must, of course, set the cookie in the same method when the user logs in.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top