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

confused about cookies 1

Status
Not open for further replies.

leadman

Programmer
Joined
Jun 11, 2001
Messages
177
Location
US
Hi all,
Ive read a few tutorial and chapters regarding session variables and cookies but am still a bit confused. Here are a couple questions i have as i try to build my first catalog site with shopping cart:

1. Can more than one cookie be created in a single appication - suppose the application is named "myapp" in application.cfm, how would i go about making cookies in this application?

2. How do I manage putting info into a cookie and changing and adding info? If the answer is 'use arrays' then do you know where I could go to learn arrays from concept to practical use?

3. When I come to Tek-tips, my name and preferences appear automatically. Exactly how is this done?
 

dealing with cookies is in it's essence very simple; you will have to work through few examples and then you will be surprised; you will have to consider using cookies for shopping cart type application; there are a lot of users that will have cookies disabled, and in that case you will loose control over the application; I am using cookies only to recognize returning visitors (where possible), but for application management I will use client or session variables that are defined and stored on the ColdFusion server; tek-tips recognize you becouse the application is plantng cookie on your system; if you delete that cookie, next time you come back you will have to log in; after you log in, your old settings will kick in; that tells you that your acctual settings were not stored in the cookie you just erased, but rather in some other place (registry, or database);

try working with client variables and cookies will become clear by themselves;
when writing cfapplication you have to specify (among other things) setclientcookies and clientmanagement attributes:

<cfapplication
name=&quot;appName&quot;
sessionmanagement=&quot;Yes&quot;
sessiontimeout=&quot;#CreateTimeSpan(0, 0, 20, 0)#&quot;
applicationtimeout=&quot;#CreateTimeSpan(1, 0, 0, 0)#&quot;

setclientcookies=&quot;Yes&quot;
clientmanagement=&quot;Yes&quot;>

to see exactly what is going on when you do that, try erasing all cookies on your system (check temporary internet files folder), and then make a requst for a template; you should see cookie writing it self in that folder

with that in working order, to create client variable, all you have to do is use regular expression:

<cfparam name=&quot;client.varName&quot; default=&quot;someValue&quot;> or

<cfset client.varName = someValue>

what happens now is harder to see, but you have just created client variable; it is stored in your registry (it is a default storage), and it will last for it's default of 90 days; if specified, you can use cookies or database to store client variable;
if possible you might want to try to work with databases (it has a lot of good aspects other then it's mobility); if you decide to work with database, after you specify data source in your ColdFusion administrator, after first request for a template, you will see two tables created automatically: CGLOBAL and CDATA;
CGLOBAL will store cfid, data and lvisit, and CDATA will store cfid, app and data; when client variable is created using cfparam or cfset, you will find the acctual variable and it's value in the data field of the CDATA table

- when you want to change the variable's value, you will simply use cfset;
- when working with shopping cart, to store temporary values (such as shopping cart content), you can use session variables

hope this will get you started Sylvano
dsylvano@hotmail.com

&quot;every and each day when I learn something new is a small victory...&quot;
 
thank you, that has helped me greatly. Could you possibly direct me on where to learn how to place and manage the many values that need to go into the shopping carts session variable (for instance, how do i create a 'table' of product data in the cart and edit it)
 
all product data that you use in your shopping cart application you can store in a database; values that you use in one session are stored in session variables; becouse you have more than one information related to the certain product, you will have to use arrays; In ColdFusion, you declare an array by assigning a variable name to the new array as follows:

<cfset newArray=ArrayNew(x)> - where x is the number of dimensions (from 1 to 3) in the array that you want to create.

example:
first, declare the full name array:
<cfset myShopCart = arraynew(1)>

add the productID to index 1 and the productPrice to index 2 of the fullname array:
<cfset myShopCart[1] = productID>
<cfset myShopCart[2] = productPrice>

poulate the array:

<cfset myShopCart[1][1]=&quot;4665asd&quot;>
<cfset myShopCart[2][1]=&quot;4.99&quot;>

so, now you have stored one product's ID# and it's price in your newly created array; to output that you will do this:

<cfoutput>
#myShopCart[1][1]# #myShopCart[2][1]#
</cfoutput>

to do this on the session level, you will have to scope your array with session prefix, e.g.:

<cfset session.myShopCart = arraynew(1)> etc...

tip:
when working with session variables, you HAVE to use CFLOCK tag;


Sylvano
dsylvano@hotmail.com

&quot;every and each day when I learn something new is a small victory...&quot;
 
Thanks Sylvano, this is very helpful - so I should put <cflock></cflock> around any code that changes the session variables? I though the session variables were unique for each user though? still need to lock?
 
this is how the cflock sentance looks like when you want to get some session variable value and scope it locally:

<cflock name=&quot;#session.sessionID#&quot; timeout=&quot;30&quot; type=&quot;readonly&quot;>
<cfset cartContent = session.cartContent >
</cflock>

or,


<cflock name=&quot;#session.sessionID#&quot; timeout=&quot;30&quot; type=&quot;exclusive&quot;>
<cfset session.cartContent = &quot;newValue&quot;>
</cflock>


you see the difference? because you can have multiple users on line at the same time, you can have multiple session variables, and each will store the value that is right value for the specific user; the &quot;session.sessionID&quot; variable within cflock tag will distinguish which session variable is about to be changed or just read; you will specify exclusive or readonly value of the type attribute, because if you have readonly value set, then more than one user can access the session variable included in the cflock tag at same time; if exclusive value is set then only one user can access session variable in question; that is important because you don't want someone to have changing session variable's value while someone else is trying to read the same variable; you will end up with corrupt data or worse...

Sylvano
dsylvano@hotmail.com

&quot;every and each day when I learn something new is a small victory...&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top