//------------------------------------------------------------------------------ // Uint: 例子/会话 //------------------------------------------------------------------------------ unit session; interface uses System.SysUtils, System.IOUtils, System.Classes, FireHttp.Utils, FireHttp.Types, FireHttp.Classes, FireHttp.HTML, FireHttp.Path, FireHttp.Log, FireHttp.Config; type example_session = class(THTMLHandler) public const __PathInfo = '/session'; const __Description = ''; protected procedure Execute; override; procedure Cleanup; override; public constructor Create(AThread: TWorkerThread); override; destructor Destroy; override; end; implementation { example_session } procedure example_session.Cleanup; begin inherited; end; constructor example_session.Create(AThread: TWorkerThread); begin inherited Create(AThread); {$region '设置处理者'} MultiPathInfo.Add(__PathInfo); Description := __Description; {$endregion} end; destructor example_session.Destroy; begin inherited; end; procedure example_session.Execute; var ul, li, br, a: THTMLElement; begin inherited; SessionStart; Document.Title.Text := 'Session'; if Request.Method = THttpMethod._GET then begin {$region 'Logout'} if Request.QueryFields.GetAsString('logout') = '1' then begin Session['username'] := ''; Session['password'] := ''; Response.Redirect('session'); Exit; end; {$endregion} if Session['username'] = '' then begin {$region 'Not logged in'} Document.Body.HTML := '
' + '' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + '
Username:
Password:
' + ' ' + ' ' + '
' + '
'; {$endregion} end else begin {$region 'Logged in'} //Session Info ul := Document.CreateElement_UL(Document.Body); li := Document.CreateElement_LI(ul); li.Text := 'SessionId=' + SessionId; li := Document.CreateElement_LI(ul); li.Text := 'Session[''username'']=' + Session['username']; li := Document.CreateElement_LI(ul); li.Text := 'Session[''password'']=' + Session['password']; //
br := Document.CreateElement_BR(Document.Body); //Logout a := Document.CreateElement_A(Document.Body); a.Attributes['href'] := 'session?logout=1'; a.Attributes['target'] := '_self'; a.Attributes['style'] := 'margin-left:40px'; a.Text := 'Logout'; {$endregion} end; end else if Request.Method = THttpMethod._POST then begin {$region 'Login'} Session['username'] := Request.ContentFields.GetAsString('username'); Session['password'] := Request.ContentFields.GetAsString('password'); Response.Redirect('session'); {$endregion} end; end; initialization RegisterHandlerClass(example_session); end.