我也是菜鳥
下面貼點我項目裡面的代碼
procedure TIWServerController.IWServerControllerBaseNewSession(
ASession: TIWApplication; var VMainForm: TIWBaseForm);
var
i: integer;
vPtr: TIWApplication;
vList: TList;
ms: string;
UserID, password: string;
begin
ASession.Data := TUserSession.Create(nil);
//------------根據config.ini中參數連接ADO
with UserSession.DM.adoconn do begin
Connected := false;
ConnectionString := '';
ConnectionString :=
'Provider=SQLOLEDB.1;' +
'Persist Security Info=False;' +
'User ID=' + CUSERID + ';' +
'Password=' + CPASSWORD + ';' +
'Initial Catalog=' + DATABASENAME + ';' +
'Data Source=' + SERVERNAME;
Connected := true;
end;
//------------剔除相同IP冗余Session,減少程式服務器Loading
vList := GSessions.LockList;
try
for i := 0 to vList.Count - 1 do begin
vPtr := TIWApplication(vList);
if (vPtr.ip = WebApplication.ip) and
(vPtr.SessionTimeStamp <> WebApplication.SessionTimeStamp) then begin
vList.Remove(vPtr);
vPtr.Free;
break;
end;
end;
finally
GSessions.UnlockList;
end;
....
procedure Tfrmbase.SaveToCookie;
begin
with WebApplication.Response.Cookies.Add do begin
Expires := now + 10;
Name := sUserID;
Value := UserSession.UserName;
end;
with WebApplication.Response.Cookies.Add do begin
Expires := now + 10;
Name := sUserPassword;
Value := UserSession.UserPassword;
end;
with WebApplication.Response.Cookies.Add do begin
Expires := now + 10;
Name := sSessionID;
Value := WebApplication.AppID;
end;
end;
procedure Tfrmbase.LoadFromCookie;
begin
with WebApplication.Request.CookieFields do begin
if Values[sUserID] <> '' then begin
if CheckUser(Values[sUserID], Values[sUserPassword]) then begin
UserSession.UserName := Values[sUserID];
UserSession.UserPassword := Values[sUserPassword];
UserSession.LastSession := Values[sSessionID];
end;
end
else begin
UserSession.UserName := '';
UserSession.UserPassword := '';
UserSession.LastSession := '';
end;
end;
end;
其中
const
sUserID = 'UserID';
sSessionID = 'LastSessionID';
sUserPassword = 'UserPassword';
可以參考 [深入探討Session] Write By 黃忠成(原文刊登於台灣Run PC!)
這篇文章,網上有下載的,如果找不到我發給你,我就是參考它做的