Всем ку!
Ну как понятно я новяк. Все делаю из под Delphi XE7.
Что есть: Форма(авторизация) и Форма(Главная).
Авторизация. Авторизация проходит на сайте через логин и пароль(IdHTTP1), получаю ответ(или + или -) Форма закрывается и открывается главная - Это я осилил. Все делаю на локалхосте.
Задача:
Как сделать что бы авторизация по кукам была? Т.е. ввел логин и пароль, форма закрылась, открылась другая форма, потом закрыл форму, тыкаешь .exe и авторизация прошла через куки, т.е. форма сама закрылась и открылась главная.
Вот:
Код:
unit Unit3;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, IdCookieManager,
IdAntiFreezeBase, Vcl.IdAntiFreeze, Vcl.ExtCtrls, Vcl.OleCtrls, SHDocVw;
type
TAuth = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Memo1: TMemo;
IdCookieManager1: TIdCookieManager;
IdAntiFreeze1: TIdAntiFreeze;
IdHTTP1: TIdHTTP;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Auth: TAuth;
LoginData, Response: TStrings;
error_text: string; // Текст "Сервер не доступен"
implementation
uses Unit1;
{$R *.dfm}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Функция парсинга
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
function Pars(T_, ForS, _T:string):string;
var a,b: integer;
begin
Result := '';
if (T_='') or (ForS='') or (_T='') then Exit;
a:=Pos(T_, ForS);
if a=0 then Exit else a:=a+Length(T_);
ForS:=Copy(ForS, a, Length(ForS)-a+1);
b:=Pos(_T, ForS);
if b>0 then
Result:=Copy(ForS, 1, b-1);
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Конец парсинга
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
procedure TAuth.Button1Click(Sender: TObject);
begin
LoginData := TStringList.Create;
Response := TStringList.Create;
try
LoginData.Add('login_email='+Edit1.Text);
LoginData.Add('login_password='+Edit2.Text);
LoginData.Add('submit=Войти');
Response.Text := idHTTP1.Post('http://localhost/auth/login', LoginData);
if pos('/auth/logout', Response.Text)>0 then
begin
Memo1.Lines.Add('Авторизация успешна!');
// Main.Show();
// Auth.Hide();
end
else
begin
error_text:= Pars('<div class="message_error">', Response.Text, '</div>');
Memo1.Lines.Add('Авторизация не удалась! Причина: ' +error_text);
end;
except
Memo1.Lines.Add('Сервер недоступен!');
end;
end;
end.