ColdFusion Programming/session variable expires
Expert: Donald Hammond - 2/19/2008
QuestionQUESTION: good day, sir donald, i created a page wherein you can see online users, online meaning the users that logged-in on that page. what i did to monitor the online users is that after each user logged-in, i update a field in a database (checkbox) to indicate that they are online. then i query all the user that have their field on the database checked. my problem is my session variable needs to be set to expire in an hour and when a user's session variable expires he is still indicated as online since his checkbox field on the database wasn't updated (since i dont know how to do it) please advice how can i show all user that has their session variable expires.
ANSWER: Aloha,
Use your application.cfc file to do this. onSessionStart will set the counter (application.online) and then onSessionEnd will subtract it.
Set application.online in your onApplicationStart method to 0 and then add 1 in onSessionStart and subtract 1 in onSessionEnd.
No need for a db field at all.
---------- FOLLOW-UP ----------
QUESTION: this is a little messy so please follow me...
im currently using application.cfm and not application.cfc,
will this matter? also here's what inside my application.cfm:
<cfif isDefined("session.CFID") and isDefined("session.CFTOKEN")>
<cfset cfid_local = session.CFID>
<cfset cftoken_local = session.CFTOKEN>
<cfinput type="hidden" name="CFID" value="#cfid_local#">
<cfinput type="hidden" name="CFTOKEN" value="#cftoken_local#">
</cfif>
<cfapplication name = "applicate"
sessionTimeout = #CreateTimeSpan(0, 1, 00, 0)#
sessionManagement = "Yes" loginstorage="session">
but i have tried what you have said, but i cant make it working, can you please write me even some bare codes for it? thank you very much and i will appriciate your response.
AnswerUnfortunately it takes a lot more work to do it outside of .cfc You need to know when the person leaves the site so you could use javascript to tell when they navigate away and pass it to CF.
The best way is to use a cfc file tho. Is there any reason you can not use cfc?