All my posts

Wednesday, October 03, 2007

Session class for SQLite

I needed Session management for a web project I made using Delphi with WebBroker, and found a unit called MISessionClass in newsgroups created by Tony Caduto. The problem with that unit was it worked with Interbase and my web hosting provider doesn't inlcude Interbase/Firebird in the package I purchased.

SQLite to the rescue

My hosting provider doesn't allow me to use Interbase/Firebird, but I can use SQLite to store my session data. To access SQLite databases, I use the exelent TLiteDb connector by Renè for DuBaron.com and adapted the unit mentioned in the previous paragraph to this database.

Some Code based loosely on the MDweb Components by Mark BrittinghamProgramming by Tony Caduto (tcad...@amsoftwaredesign.com)May 24th, 2001This class uses a IBexpress database,transaction and query which are created in theconstructor.To use you must create a table on some interbase server (it could be on thesame pc as the webserver or on a remote server) called SESSIONS whichcontains three fields:

SESSIONID (integer)SESSTIMESTAMP (Timestamp)SESSDATA (blob)

In the constructor set your database name IE www.myserver.com:c:\yourpath\yourdatabase.gdband the SQL dialect

Add a public declaration for the class IEPUBLIC SessionMgr:TMISession;

In your webmodule add the following:in the oncreate eventSessionMgr:=TMISession.create;in the ondestroy event add:SessionMgr.free;

Use is very similar to MDweb without all the extra stuff IE special tags etcThis class works with the standard Webbroker pageproducer.

In the before dispatch add something like this: if request.queryFields.Values['islogon'] <> 'true' then begin SessionMgr.SessionID := StrToIntDef(Request.CookieFields.Values['SID'], 0); if (Not SessionMgr.FindSessionRecord) then Response.SendRedirect('http://www.yourserver.com/timeout.htm'); end;