A reader asks:
Is there anyway to share variables between two different applications within one website?
Actually there is a very simple, but slightly dangerous way - the Server scope. As you can guess, the Server scope is global to the ColdFusion server. So any application could set a Server variable and any other application could read it. So why is this dangerous?
-
If you are hosting your sites on an ISP, it is very easy for another customer to view and modify the Server scope. While they probably wouldn't, you can't be certain that your data won't be changed by others. If you use this route, I would only suggest it in a case where you have the entire ColdFusion server to yourself. To be anal, even Application variables aren't safe. If someone can guess the name of your application scope, they can just use the <cfapplication> tag to switch to your application and read/modify values.
-
If you have multiple applications reading and writing to the server scope, then you definitely want to ensure you use <cflock>. If you don't, there is a risk of data getting messed up and corrupted. At least (thank god!) you won't have the memory issues that plagues pre-MX versions of ColdFusion.
I'd highly recommend you build a CFC to make this easier. You can imagine a CFC whose sole purpose is to read and write to the Server scope. This makes your code easier. You can do something like so:
<cfset theVal = application.serverFacade.read("numberpirates")>
Inside the CFC the locks would be handled for you. Ditto for a write:
<cfset application.serverFacade.write("status","Klingons on the starboard bow.")>
What's nice is that if you change your mind about how the data is persisted, your client code doesn't have to change, just your component.