Our online blog

Archive for the ‘ColdFusion/Railo & MySQL’ Category

[Tutorial] ColdFusion Basic Sessions

Wednesday, November 19th, 2008

As Sessions is a big deal I have decided to make parts to it. In part 1 we are going to see how to detect if a session is undefined and to create it.

First thing you need to know is that sessions uses structures, you can have multiply structures for your website for different things.

We are going to work up a simple login system using sessions. We are calling the structure loggedin. The first step is to see if the session has been created, so we will need some if statements for the session detect:

Quote:
<cfif not isDefined(‘SESSION.loggedin’)>
</cfif>

Next we want to create the session if its not defined, the code above is shown below but with cfset tags:

Quote:
<cfif not isDefined(‘SESSION.loggedin’)>
<cfset SESSION.loggedin = StructNew()>
<cfset SESSION.loggedin.username = test>
</cfif>

Now what we have asked coldfusion to do is create a new structure (StructNew) with ‘SESSION.loggedin’ as the structure name. After this we want to populate the session structure with a value within the ‘username’ session. We have just used test for now but we go onto bringing in database from a database to work with a login script.

[Tutorial] ColdFusion date and time

Tuesday, November 18th, 2008

I am going to be going into how to get the current date & time with coldfusion, dont worry it is very simple to do and does not require anything special like a datasource.

Date
First we are going to be working with getting the date onto your page.

Step 1
First within your Application.cfm/.cfc we want to add the following cfset to get the date. If you wish you can add this to the same page as the output of the date but it is always nice to have an application file.

Quote:
<cfset TheDate = Now()>

Step 2
The following code mainly formats how the date gets displayed, the sample code below will output the date in the format of YYYY/MM/DD but you can easily turn this into: DD/MM/YYYY.

Quote:
<cfoutput>
#DateFormat(TheDate, ‘yyyy-mm-dd’)#
</cfoutput>

Time

Step 1
Just like the date we want to add the following code to detect what the current time is

Quote:
<cfset TheTime = Now()>

Step 2
Again same as the date we want to format the time in to something we can display and read on screen. There is something that is very different to the date and that is using capitol letters, when you have for example ‘HH’ instead of ‘hh’ for the hour to are saying to the coldfusion to use a 24 hour system with the hours. Below we have set the time to display in a 24 hour clock with HH:MM:SS you can take out the ‘SS’ if you do not wish to display the seconds.

Quote:
<cfoutput>
#TimeFormat(TheTime, ‘HH:mm:ss’)#
</cfoutput>

Thats it for time and dates