Dec 19, 2007

Simple session and setting sessions with cfset example

As part of my effort for creating simple Coldfusion examples. I want to take about variables. As we know in a normal website we are taught to pass URL or FORM variable from page to page. This is great for a one or two things or for a form of variables/values. So there is another very useful way to pass variables and that is by sessions. I won’t explain what a session 'is' except that it works with cfset tag like normal variables.

Let me show you

URL variables look like this when you pass it from a URL query string.

http://www.sitename.com/index.cfm?binky=boink

then you pull it into the the target page like this:

<cfoutput>
#url.binky#
</cfoutput>

You don't need the value after the equal sign because when the page resolves it will show on the screen.

imagine a form on a page that has a textbox input area. The label of the this 'input' text field is called 'name'. Then you type in the word 'frank' and hit submit.

It passes the value to a page called formCatch.cfm

You would display it like this.

<cfoutput>
#form.name#
</cfoutput>

It would output the word frank.

And now lets look at a session:

<cfset session.name = 'frank'>

<cfoutput>
#session.name#
</cfoutput>

It would output the name Frank, but here is the good thing about sessions, you can go to any page after setting the session variable and call it.

So lets go back to the FORM and URL variables. those are great for one page justs, but after that they loose purpose.

sessions stay for the duration of your access to the website or until you explicitly tell it togo away.

Here is how you do that.

<cfset StructDelete(Session, "name")>

One other note. Before you can use sessions in your website you have to 'tell' it that it will use sessions.

Here is how that works.

In your web application directory you can create a blank .cfm file called 'Application.cfm'. Put this file on the directory where you will use sessions.

Then add this line of code:

<cfapplication sessionmanagement="yes">

So that is sesssions in a nutshell. This should get anyone by with sessions.

Frank

Labels: , , , , , , ,

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home