Link to home
Start Free TrialLog in
Avatar of Bradino
Bradino

asked on

Check if a coldfusion session still exist

Dear Experts,

Im want to properly display a list of member usernames currently online. The way that Im currently doing does not seem to work. Im using Coldfusion session to do this, here is my MSSQL database structure.

USERS TABLE:
Id (int- auto increment)
Username (varchar 20)
Password (varchar 15)
Online_status (tinyint default 0)
Session_id (varchar(50)
Session_time (smalldattime)

<CFAPPLICATION NAME="activeUser"
CLIENTMANAGEMENT="YES"
SESSIONMANAGEMENT="YES" SESSIONTIMEOUT="#CreateTimeSpan(0,0,5,0)#">

1.      when user successfully logs in:
a.      update the online_status to 1
b.      update the session_id with CFID
c.      update the session_time with timeformat(now())

When the members actually click on the logout button I simply:
2.      update online_status to 0

Now the problem is that when member DONT log out or idle for hours. Ive tried several way but had no luck, I cant seem to figure this out.

What I tried was to loop through the user table with this query below and see if there session_id still exist. But the syntax that I used was incorrect.

Select *
From users
Where online_status = 1

HELP please! Thank you in advance for your help.
Avatar of rob_lorentz
rob_lorentz

you need to create a stored procedure that can be scheduled to run hour, every half hour, every 10 minutes, whatever. That sp would update the online_status flag to 0 if the session time is older than 2 hours or whatever your coldFusion session timeout is set to.

in our user table we have a sessionStart time and a sessionLastRequest time. Everytime the user requests a page we verify their session and update the lastRequest time. we check the lastRequest times to determine which users are no longer attached and update/delete them.
Avatar of Bradino

ASKER

Rob,

Isn't  there a way to do this in the application.cfm onEndRequest? The reason why i ask is I'm unfamiliar with sp and not sure where to begin.

thank for your reply!
i dont think i would put it in the application.cfm. because that would subject all users to the check. you could do it via coldFusion in a schedule .cfm file that executes on a scheduled basis.

either way you need to add a lastRequestTime field to your users table and update it in application.cfm
Avatar of Bradino

ASKER

Rob,

I sorry, this maybe a stupid question but how do i check every time a user request a page and how do verify their session and update the lastRequest time?
Avatar of Bradino

ASKER

Could you please provide a sample script? Thank you!
in your application.cfm where you verify that the user is currently logged in, update their contact record with the request time.
Avatar of Bradino

ASKER

Here what i have in my application.cfm.

<CFAPPLICATION NAME="activeUser"
CLIENTMANAGEMENT="YES"
SESSIONMANAGEMENT="YES" SESSIONTIMEOUT="#CreateTimeSpan(0,0,5,0)#">

<CFPARAM NAME="session.Allowin" DEFAULT="false">

thats it, not sure what your saying.
ASKER CERTIFIED SOLUTION
Avatar of rob_lorentz
rob_lorentz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bradino

ASKER

Thanks for all your help, but I'm about to just give up. I did exactly what you suggested and it's not update the lastRequestTime.


<CFAPPLICATION NAME="activeUser" SESSIONMANAGEMENT="YES" SESSIONTIMEOUT="#CreateTimeSpan(0,0,5,0)#">

<CFPARAM NAME="session.Allowin" DEFAULT="false">

<CFIF session.Allowin>
  <CFQUERY NAME="stampUser" DATASOURCE="testDSN">
    update users
         set lastRequestTime = #now()#
    where id = #session.userID#
  </CFQUERY>
</CFIF>

Question when a user link on a link on the page it should trigger the update, right?

thank again!
Avatar of Bradino

ASKER

I finally got it to work! Thank you for pointing me in the right direction. I was wondering if you can answer this question? is it recommended to use application.cfm or application.cfc or both?

thanks again!
we use application.cfc, gives you a bit more functionality, with the onRequestEnd, etc.

but be careful, if you do a cflocation in your code anywhere, the onRequestEnd doesnt get called. it only gets called when the request completes entirely.

Avatar of Bradino

ASKER

Great, thanks for the tip.