0

Since the system I am using has Login and Logout feature, I am inside Session when I Logs in to the system. I am new to Session, my question is whatever variable and its value I have defined in any coldfusion page, would I be able to use it on any page?

For example, while going through the code of my system, I came across the following line one each and every CFML page:

<cfparam name="INPUTID" default="0"> 

and then later on somewhere in the page, I have seen this variable getting used like #INPUTId# .

Please clarify

3
  • 3
    That code above nothing to do with sessions. It simply sets a default value for a variable. Take a look at the docs on cfparam and also scope types. Unscoped variables, like the one above, are usually placed in the variables scope by default. Commented Nov 1, 2013 at 21:51
  • I suggest minimizing the use of session variables. The reason is that with modern browsers, it is possible for these to be changed by a user in a way that you did not anticipate. Commented Nov 1, 2013 at 23:04
  • @DanBracuk Session variables in ColdFusion may be different from what you are thinking of Commented Nov 2, 2013 at 2:50

1 Answer 1

4

To answer the question "whatever variable and its value I have defined in any coldfusion page, would I be able to use it on any page" ... that depends.

If you set a session variable e.g. <cfset session.foo = "bar" > then you can call #session.foo# on any page since it will be stored in the user's session.

However if you simply set a value, e.g. <cfset foo="bar" > then it will end up in the 'variables' scope and only available within that page, or request. (on that note, CF has a specific "request" scope, e.g. request.foo, which is for this purpose, available throughout any code that comes after the place where the value is set, in the same request or page view).

So, if you want to set values that can be used on other pages, use the session. But be careful, you will also need to use cfparam to set defaults, or use structKeyExists() to check for the value, before trying to call it from the user's session, since the value may not exist unless it has been set already. Otherwise, for values used in the same page, use the 'request' scope, or see the CF docs for other scopes e.g. variables, local, etc.

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm, even I was thinking same that it should have something like session.foo etc but still it doesn't have anything and it's getting a value from somewhere. anyways, thanks, I am figuring out what's going on here as lot of cfc's are included in my file. Thanks again.
It is often better for something to exist and be blank. That way you don't have to test for existence and non blankness
On the other hand, if something should exist and doesn't, that might mean that the page was not accessed the proper way and appropriate action should be taken.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.