How can I use session variables to determine the date of a user's last visit?

While the client scope has this functionality built in (client.lastVisit), with the session scope you will need to set your own variable to track a user's last visit date. On your Application.CFC page's onRequestStart() method, set a session variable with the current date time. This session variable will then be updated on each new page request.

<cffunction name="onRequestStart" returnType="boolean">
	<cfset session.lastVis =  now()>
</cffunction>

Remember that session variables will usually timeout much sooner then client variables (based on server or local settings), and once a session is timed out, you will loose that user's last visit session variable. With this in mind, you may want to consider adding some code to your Application.CFC page's onSessionEnd() method to write the session data to a database or file on session end.

<cffunction name=" onSessionEnd" returnType="boolean">
	-insert code to persist session.lastVis to DB or File-
</cffunction>

This question was written by Jeremy Petersen
It was last updated on June 30, 2006.

Categories

Application Management

Comments

comments powered by Disqus