Skip to main content
Inspiring
May 23, 2006
Answered

Need Help: Understanding Session Variables

  • May 23, 2006
  • 2 replies
  • 556 views
Greetings -

Perhaps I am just not understanding how session variables function and
hopefully someone here can assist me in doing so.

I am trying to establish an auto report numbering process for a customer
so that when a person displays a report online, a report number is
generated and stored as a session variable and when all information
is submitted, upload report number and the additional submitted information
to a database. All appears to be working as intended, with the exception of this.

I am using an application.cfm file to set session management and to create a
report number and set that number to a session variable.

When the person displays the report form page, there is an instructional section
for their review, followed by a "Proceed" button to go onto the next section of the
page.

When they click on the "Proceed" button, is when the report number held in a
session variable (application.cfm) should insert into a hidden form field for later
upload, which appears to be occurring.

Now for the part that I don't understand. I thought that as long as a session
had not timed out, the session variable would remain for use and thus if
the report page was refreshed (reloaded) the same session variable would
be used.

What I am experiencing, is that each time the report page is being refreshed,
a new report number is being loaded into the session variable. Am I not
understanding how session variables work? Am I missing some critical
piece of coding or point here?

I have included below the folder structure and files used in the process
for review.

Thanks in advance for any assistance to making this work.

Leonard B

===============================================
===============================================

Folder Structure
-- domain/reports
-- domain/reports/input/

Files
-- domain/reports.cfm <- Entry Point to process
-- domain/reports/application.cfm
-- domain/reports/input/report_form.cfm

====================
application.cfm

<cfapplication name="StartApp" sessionmanagement="yes"
sessiontimeout="#CreateTimeSpan(0,0,20,0)#"
applicationtimeout="#CreateTimeSpan(0,0,2,0)#">

<cfset DSN = "datasource">

<cfquery name="getnumber" datasource="#DSN#">
SELECT report_number
FROM report_numbers
</cfquery>

<cfset Session.report_number = #getnumber.report_number#>

<cfif getnumber.recordcount IS 0>
<cfquery name="insertnumber" datasource="#DSN#">
INSERT INTO report_numbers (report_number) VALUES (1)
</cfquery>
<cfelse>
</cfif>

<cfquery name="updatenumber" datasource="#DSN#">
UPDATE report_numbers
SET report_number = report_number + 1
</cfquery>

====================
report_form.cfm

<cfparam name="Button" default="Start">

<cfif #Button# is "Start">

<cfform action="report_numbers.cfm" method="post" enctype="multipart/form-data">
<input type="submit" name="Button" value="Proceed" />
</cfform>

<cfelseif #Button# is "Proceed">

<div style="padding: 50px 5px 15px 5px">
<cfoutput>#Session.report_number#</cfoutput></div>

</cfif>
    This topic has been closed for replies.
    Correct answer Dan_Bracuk
    quote:

    Originally posted by: Leonard B
    Hi Dan,
    Thanks for the response, let me clarify what the goal is. This numbering process
    is for a public service organization. The goal is not to establish a primary key,
    but to establish a sequential numbering process for completing online reports that
    can be reset to zero at the beginning of each year.

    I am certainly open minded to doing things in an easier manner and if you have an
    easier way, I am all ears or should I say all eyes. However, with the requirement of
    resetting the process back to zero, I could not come up with any other means of
    handling the task at hand.

    Thanks

    Leonard B

    In that case it would be stil be easier to run the "next number" code right before you insert your data. From what you have said so far, that is the only time you actually need it.

    But if you want to stick with the application.cfm approach, there is a difference in what you say you are doing and what your code shows. You say "I have checked to see if the session variable has been defined and it has been. ", but, I don't see any if/else logic in the part of your application.cfm code where you set the variable. Just remember, that code runs on every page request, including refreshes,

    2 replies

    Inspiring
    May 23, 2006
    You are doing way too much work. If the ultimate goal is to generate "the next number" to use as a primary key in a database record, it's a lot easier to get the number at the end of the process rather than the beginning. It is also easier to let your database do the work for you instead of maintaining your report_numbers table.
    Leonard_BAuthor
    Inspiring
    May 23, 2006
    Hi Dan,

    Thanks for the response, let me clarify what the goal is. This numbering process
    is for a public service organization. The goal is not to establish a primary key,
    but to establish a sequential numbering process for completing online reports that
    can be reset to zero at the beginning of each year.

    I am certainly open minded to doing things in an easier manner and if you have an
    easier way, I am all ears or should I say all eyes. However, with the requirement of
    resetting the process back to zero, I could not come up with any other means of
    handling the task at hand.

    Thanks

    Leonard B
    Dan_BracukCorrect answer
    Inspiring
    May 24, 2006
    quote:

    Originally posted by: Leonard B
    Hi Dan,
    Thanks for the response, let me clarify what the goal is. This numbering process
    is for a public service organization. The goal is not to establish a primary key,
    but to establish a sequential numbering process for completing online reports that
    can be reset to zero at the beginning of each year.

    I am certainly open minded to doing things in an easier manner and if you have an
    easier way, I am all ears or should I say all eyes. However, with the requirement of
    resetting the process back to zero, I could not come up with any other means of
    handling the task at hand.

    Thanks

    Leonard B

    In that case it would be stil be easier to run the "next number" code right before you insert your data. From what you have said so far, that is the only time you actually need it.

    But if you want to stick with the application.cfm approach, there is a difference in what you say you are doing and what your code shows. You say "I have checked to see if the session variable has been defined and it has been. ", but, I don't see any if/else logic in the part of your application.cfm code where you set the variable. Just remember, that code runs on every page request, including refreshes,

    Inspiring
    May 23, 2006
    You should check if the session varible has been defined
    <cfif not isdefined('#Session.report_number #'>
    <cfset Session.report_number = #getnumber.report_number#>
    </cfif>
    Leonard_BAuthor
    Inspiring
    May 23, 2006
    HI Jim,

    I have checked to see if the session variable has been
    defined and it has been.

    Thanks

    Leonard B