Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I check for a session? 2

Status
Not open for further replies.

SteelDragon

Programmer
Feb 1, 2001
134
US
I need to know how to check to see if there is a session active(any session for any user) in a particular CF app, and if not(no users logged in) I want to perform an action to delete data from my DB. How could I set up a CF page to do this automatically, either when a person logs out, or at specified intervals.

Thanks,
SteelDragon
 
Hey Steel,

I'm not aware of any way to get session information across a site. You might want to set a server or application variable on each page request from the application.cfm file. You could do something like this <cfset application.lastRequest = now()> and then check to see when the last request was made. If it's greater than your session time (use datediff function), you know there are no active sessions.

Hope this helps,
GJ
 
Sure, here is the code. But you would have to do this for every application.cfm file.

CFPROGRAMMER
application.cfm
------
<cfset theTimeout = CreateTimespan(0,0,30,0)>

<cfapplication name=&quot;Extranet&quot;
clientmanagement=&quot;Yes&quot;
sessionmanagement=&quot;Yes&quot;
setclientcookies=&quot;Yes&quot;
sessiontimeout=&quot;#theTimeout#&quot;>




<!--- Session-Tracker Code --->

<cflock name=&quot;#APPLICATION.applicationName#&quot;
type=&quot;Exclusive&quot;
timeout=&quot;20&quot;
throwontimeout=&quot;Yes&quot;>

<!--- If Session-Tracker does not exist,
generate it --->
<cfparam name=&quot;APPLICATION.SessionTracker&quot;
default=#StructNew()#>

<!--- Log current user --->
<CFSET dummy = StructInsert(APPLICATION.SessionTracker, CGI.REMOTE_ADDR, Now(), true)>

</cflock>


----


See Who is on,
------

<HTML>
<TITLE>OrlandoInfo</TITLE>
<HEAD>
<STYLE type=&quot;text/css&quot;>font { font-family: Arial, sans-serif, Helvetica }
body { font-family: Arial, sans-serif, Helvetica }
table { font-family: Arial, sans-serif, Helvetica }
td { font-family: Arial, sans-serif, Helvetica }
</STYLE>
</HEAD>
<BODY bgcolor=&quot;White&quot; background=&quot;images/gauzeyellow.gif&quot; link=&quot;Black&quot; alink=&quot;#444444&quot; vlink=&quot;Black&quot;>
<P>
<TABLE width=&quot;96%&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; border=&quot;0&quot;>
<TR>

<TD valign=&quot;bottom&quot; width=&quot;17&quot;><IMG src=&quot;images/watch.gif&quot; width=&quot;17&quot; height=&quot;13&quot;>
</TD>

<TD valign=&quot;center&quot;> <FONT color=&quot;#000000&quot;><B> <font size=&quot;-1&quot;>Live content
created <cfoutput>
#TimeFormat(now(), 'HH:mm:ss')# #DateFormat(now(), &quot;ddd mmm dd, yyyy&quot;)# </cfoutput>
(Server Time)</font> </B></font> </TD>
</TR>
</TABLE>

<table width=&quot;475&quot;>
<TR>
<TD bgcolor=&quot;#224499&quot; background=&quot;images/bargauzeblue.gif&quot;>

<FONT size=&quot;-1&quot; color=&quot;#FFFFFF&quot;>
<cfoutput>

<B> &nbsp;&nbsp; Whos On - #cgi.server_name# has #StructCount(APPLICATION.SessionTracker)# active sessions. </cfoutput>
</B></font>
</TD>
</TR>
</table>

<cfoutput>
<h3>
<cflock name=&quot;#APPLICATION.applicationName#&quot;
type=&quot;Exclusive&quot;
timeout=&quot;20&quot;
throwontimeout=&quot;Yes&quot;>
</cflock>
</h3>
<cflock name=&quot;#APPLICATION.applicationName#&quot;
type=&quot;Exclusive&quot;
timeout=&quot;20&quot;
throwontimeout=&quot;Yes&quot;>
<table border=&quot;0&quot; cellspacing=&quot;1&quot; width=&quot;475&quot; cellpadding=&quot;1&quot;>
<tr bgcolor=&quot;##cccccc&quot;>
<td background=&quot;images/bargauzeblue.gif&quot;><b><font size=&quot;-1&quot; color=&quot;##FFFFFF&quot;>User
(by IP-address)</font></b></td>
<td background=&quot;images/bargauzeblue.gif&quot;><b><font size=&quot;-1&quot; color=&quot;##FFFFFF&quot;>Session-Status</font></b></td>
</tr>
<CFLOOP collection=#APPLICATION.SessionTracker# item=&quot;aUser&quot;>
<cfset onlineSince = StructFind(APPLICATION.SessionTracker, aUser)>

<CFIF DateCompare(onlineSince+theTimeout, Now()) EQ 1>
<!--- UserÕs last activity lies within session-timeout,
so his/her session is active --->
<cfset inactiveSince = DateDiff(&quot;n&quot;, onlineSince, Now())>
<!--- The threshold for coloring the report
may be set individually: --->
<cfif inactiveSince gte 20>
<cfset theColor = &quot;Red&quot;>
<cfelse>
<cfset theColor = &quot;black&quot;>
</cfif>



<TR>
<TD colspan=&quot;3&quot; nowrap valign=&quot;MIDDLE&quot; bgcolor=&quot;##000000&quot;><IMG align=&quot;LEFT&quot; vspace=&quot;1&quot; hspace=&quot;1&quot; border=&quot;0&quot; src=&quot;images/bchart.gif&quot; width=&quot;17&quot; height=&quot;17&quot; alt=&quot;View Detailed Report&quot;>
<FONT size=&quot;-1&quot; color=&quot;##FFFFFF&quot;> &nbsp;<b>#aUser#</b>
<cfif cgi.remote_addr eq auser>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=&quot;red&quot;>This Is You!!</font>
</cfif>

</font><b></b></TD>
</TR>
<TR>
<TD nowrap bgcolor=&quot;##9bb0d4&quot; align=&quot;right&quot;><FONT size=&quot;-2&quot;> <B> <font size=&quot;-1&quot;>Session
Status:&nbsp; </font> </B></font> </TD>
<TD align=&quot;left&quot; bgcolor=&quot;##bcd0f0&quot;> <FONT size=&quot;-1&quot;><font color=&quot;#thecolor#&quot;>
Inactive</font> for <b>#inactiveSince#</b> mins </font> </TD>
</TR>
<CFELSE>
<!--- UserÕs session has timed-out, so we can
delete his IP from the Session-Tracker --->
<cfset dummy = StructDelete(APPLICATION.SessionTracker, aUser)>
</CFIF>
</CFLOOP>
</table>
<p>&nbsp;
</cflock>
</cfoutput> </HTML>
 
I've you're using cfmx 7 it supports functions called (i think) "onSessionStart" and "onSessionEnd" which you could probably use to add and subtract 1 from an application variable. asp does this easily in the global.asa file.

Technology is dominated by two types of people: those who understand what they do not manage, and those who manage what they do not understand. - Putt's Law
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top