Reputation: 43
Is it possible to define a session variable using another variable as the name of the session variable? I have not found anything about this situation when researching how to define a session variable.
I am a novice CFML user, and here is the situation I am attempting to set up. I hope it's not too wordy and confusing.
I have code on each page that shoots me an email alert when a given site user accesses a particular page during the user's session. Works fine. But, I want that email alert triggered only the first time the user accesses the page -- I don't care about additional page visits to that page by that user during that user's session.
I need to define a session variable unique to "that user/that page" combination. Whenever any user accesses that page, I look for that variable. If there is a match, it means that user already visited that page during that user session, and I don't trigger an email alert.
For the "that user" part, I have already defined a session variable for the user from when the user logged in (Session.Name).
For the "that page" part, I already define a unique variable at the top of each page - example
<cfset page_filename = 'index.cfm'>
Conundrum: How to combine both parts (Name and page_filename) into a single unique session variable when one of the two parts (page_filename) is itself a variable.
So far, I've tried defining "Session.page_alert" using page_filename with Session.Name
<cfset Session.page_alert = '#page_filename##Session.Name#'>
But, this becomes useless as soon as the user visits another page, since the #page_filename# part is different on each page, and therefore the unique session variable pertains to the new page and not the previous page. If the user returns to the previous page, the Session variable no longer refers to it, so it appears as if the user is visiting that first page again even when the user had already been there.
Ideally, I would want to do something like
<cfset Session.#page_filename# = '#page_filename##Session.Name#'>
=============================================================== `
<!--- username variable Session.Name is defined upon user login --->
<!--- do a test display for existence of this variable --->
<cfoutput>
<p>SESSION.NAME = #Session.Name#
</cfoutput>
<!--- this part works fine --->
<!--- define page_filename local variable at top of each page --->
<cfset page_filename = 'index.cfm'><!--- example for one page --->
<!--- do a test display for existence of this variable --->
<cfoutput>
<p>PAGE_FILENAME = #page_filename#
</cfoutput>
<!--- this part works fine --->
<!--- attempt to define session variable using the page_filename variable defined above --->
<cfset Session[page_filename] = '#page_filename##Session.Name#'>
<!--- test display after attempt to define session variable --->
<cfoutput>
<p>SESSION.PAGE_FILENAME = #Session.page_filename#
</cfoutput>
<!--- this produces a CF error msg stating that "page_filename is undefined in Session" --->`
Upvotes: 1
Views: 124
Reputation: 32885
<cfset Session[page_filename] = '#page_filename##Session.Name#'>
Update Here's a modification to your code with Request
scope in place for Session
scope. Please compare this with what you have by changing the code to Session
scope.
<!--- username variable Session.Name is defined upon user login --->
<cfset request.Name = "Username Foo">
<!--- do a test display for existence of this variable --->
<cfoutput>
<p>request.NAME = #request.Name#
</cfoutput>
<!--- this part works fine --->
<!--- define page_filename local variable at top of each page --->
<cfset page_filename = 'index.cfm'><!--- example for one page --->
<!--- do a test display for existence of this variable --->
<cfoutput>
<p>PAGE_FILENAME = #page_filename#
</cfoutput>
<!--- this part works fine --->
<!--- attempt to define session variable using the page_filename variable defined above --->
<cfset request[page_filename] = '#page_filename##request.Name#'>
<!--- test display after attempt to define session variable --->
<cfoutput>
<p>request.PAGE_FILENAME = #request[page_filename]#
</cfoutput>
Run me on TryCF: http://trycf.com/scratch-pad/pastebin?id=W6KSYwZc
Upvotes: 5
Reputation: 7519
I think the cleanest (in terms of reading the code and understanding what is going on) way of doing this is:
<cfset session[page_filename] = page_filename & session.Name />
Upvotes: 4
Reputation: 1
and you can also use
<cfset "session.#page_filename#" = "#page_filename##Session.Name#" />
Hope this helps!
Upvotes: 0