Skip to main content
Known Participant
December 12, 2008
Question

onSessionEnd delete username from applicationlist

  • December 12, 2008
  • 8 replies
  • 1248 views
Hi everyone,

I need some help with the OnSessionEnd function within my Application.cfc, let me explain:
When users log in to the website their username gets stored in an application list 'users' (so I can show the users currently online on the website, for example.). When a user clicks the logout button I delete them from that list, everything working like it should be here. But when users just close their browser window they don't get logged out, so I have set a sessiontimout value and use an OnSessionEnd function.

Now my question is, how do I delete the user from the list within the OnSessionEnd function? How do I search for a user that is already gone -> can't search for #GetAuthUser()# since he is already gone).

I hope you understand what I'm trying to say :)

Anyway thanks in advance any help is much appreciated!
This topic has been closed for replies.

8 replies

BKBK
Community Expert
Community Expert
December 23, 2008
There is some confusion. Your code has at least 4 structures to hold a user's authentication details. Examples are SESSION.u_naam, FORM.loginusername, login.ledenNickname and, possibly, Coldfusion's built-in cflogin.name. There is no need for such complexity.

I suspect you're confusing login.userName with the notion of cflogin.name. In the login form, change the name attribute of the username and password input fields, respectively, to j_username and j_password.

When the form is submitted Coldfusion will automatically create the structure cflogin. That would enable you to write

<cfset SESSION.u_naam = cflogin.name>
<cfset SESSION.u_ip = CGI.REMOTE_ADDR>
<cflogin>
<cfloginuser name='#cflogin.name#' password='#hash(cflogin.password)#' roles='#login.ledenRechten#'>
<cfset application.users = ListAppend(application.users, '#cflogin.name#(#CGI.REMOTE_ADDR#)')>
</cflogin>

BKBK
Community Expert
Community Expert
December 22, 2008
Is the code containing the cflogin tag in onRequestStart? I think it should be.


BKBK
Community Expert
Community Expert
December 17, 2008
WouterDB,

I've just seen at least one reason why the code wont work properly. The list includes the user's IP, whereas the findUser check does not. That part of the code should be something like

<cfset findUser = ListContains(application.users, '#GetAuthUser()#(#CGI.REMOTE_ADDR#)', ";")>
<cfif findUser EQ 0>
<cfelse>
<cfset application.users = ListDeleteAt(application.users, findUser, ";")>
</cfif>
<cflogout>




WouterDBAuthor
Known Participant
December 22, 2008
Thanks for the comments but it's not working yet, I modified the code a little bit, this is what I have now:

on login:
<cfset #SESSION.u_naam# = #FORM.loginusername#>
<cfset #SESSION.u_ip# = #CGI.REMOTE_ADDR#>
<cflogin>
<cfloginuser name='#login.ledenNickname#' password='#hash(login.ledenPaswoord)#' roles='#login.ledenRechten#'>
<cfset application.users = ListAppend(application.users, '#SESSION.u_naam#(#SESSION.u_ip#)')>
</cflogin>

application.cfc onsessionend:
<cffunction name="onSessionEnd" returnType="void" output="false">
<cfset findUser = #ListContains(application.users, '#SESSION.u_naam#(#SESSION.u_ip#)')#>
<cfif findUser EQ 0>
<cfelse>
<cfset application.users = #ListDeleteAt(#application.users#, #findUser#, ",")#>
</cfif>
<cflogout>
</cffunction>

I made a 'checkpage' to see what's in the list application.users and what's in the sessions u_naam and u_ip, when my session ends (put a 10 second timeout) and I go to this page, I see that the application.users list hasn't changed, but I do get an error that the sessions u_naam and u_ip are not found... (so the session really did end).
The only thing I can think of is that my "onSessionEnd" code in the application.cfc isn't executed... I do a ListContains in that function and I already tried to set the application.users list to "bleh" if the findUser variable was 0 and even that didn't work :), it just doesn't get executed I think.

Any help? :)

Thx
Inspiring
December 16, 2008
> Now my question is, how do I delete the user from the list within the
> OnSessionEnd function? How do I search for a user that is already gone -> can't
> search for #GetAuthUser()# since he is already gone).

Store the value getAuthUser() gives you as a session variable, and then use
that to reference the record in your application-scoped list in the
OnSessionEnd event handler.

--
Adam
Inspiring
December 15, 2008
Hi,

Have you tried the code?.. What error does it throw?.
WouterDBAuthor
Known Participant
December 15, 2008
Hi,

It doesn't throw an error, it just doesn't work :-)
(Probably because I don't have a "UserStruct" defined? To explain it really basic, I need a code kinda like this one http://www.adobe.com/cfusion/webforums/forum/whoson.cfm, the only problem with mine is that when a user logs on he stayes logged on, forever. :) Except when he clicks the "logout" button then he is deleted from that list...
WouterDBAuthor
Known Participant
December 12, 2008
no errors, but it just doesn't delete the user from the list when the session ends apparently... probably since it searches in the list for #GetAuthUser()# (same code as on the logout page) and it just doesn't find the #GetAuthUser# so it doesn't delete one...
Inspiring
December 12, 2008
Hi,

Try if this works, Modify this code as needed,
WouterDBAuthor
Known Participant
December 15, 2008
What do I have to put instead of "UserId"? I can't find the userid of a person already gone from the website, can I ?
Inspiring
December 12, 2008
What happens with your present code?.. Does it yield any error?
Inspiring
December 12, 2008
Hi,

Can you please post your code here?
WouterDBAuthor
Known Participant
December 12, 2008
Hi,

So, when the user logs in, this code is executed:

"..."
<cflogin>
<cfloginuser name='#login.ledenNickname#' password='#hash(login.ledenPaswoord)#' roles='#login.ledenRechten#'>
<cfset application.users = application.users & '#login.ledenNickname#(#CGI.REMOTE_ADDR#);'>
</cflogin>
"..."

explanation: cflogin is executed and the username + ip-address is put in an application list

When a user clicks the logout button he is logged out and gets removed from the list:

<cfset findUser = ListContains(#application.users#, '#GetAuthUser()#', ";")>
<cfif findUser EQ 0>
<cfelse>
<cfset application.users = #ListDeleteAt(#application.users#, #findUser#, ";")#>
</cfif>
<cflogout>

Now I need a code to put in the OnSessionEnd function of my application.cfc to delete the user from the list when the session ends/times out...