Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Session closing after click on href link to load new page



Permlink Replies: 11 - Last Post: Jan 10, 2017 6:28 AM Last Post By: Guest
Guest
Session closing after click on href link to load new page
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 6, 2017 9:37 PM
Hi, I have a IWLoginForm, IWFormFirst and IWFormSecond. I use template forms in all my application.

After success login, I save some informations(username, passw, islogged) into Usersession and then I load IWFormFirst.

IWFormFirst has a link as:

   <h4><a href="start/?aparam=secondform&nomemed=XXXX&nm=1>MAMAMA"</a></h4>


to load IWFormSecond.

The problem is that when I click this link the current session is closed and a new Session is triggered. Then I lost all my informations in Usersession.
I put a breakpoint in OnCloseSession and I can see it.

First, I don´t understand why my current session is closed and a new session is opened when I click that link.
Second, I create a class TPCPSession = class(TIWUserSession) to save the data of the current user logged into session. Is it correct to do it?

Could someone check what am I doing wrong?


PseudoCode:

IWLoginForm is initially the main form. I do:

proedure TIWLoginForm.checklogin();
begin
  if loginOK then begin
     IWFormFirst.Create(WebApplication).Show;
     Usersession.Username='John';
     Usersession.Password='Doe';
     Usersession.isLogged=True;
 end else webpapplication.showmessage('wrong user');
end; 


On IWFormFirst I have:
procedure TIWFormFirst.IWAppFormCreate(Sender: TObject);
var  t:string;
begin
      t:=t+ '<div class="caption">';
      //this link load the second form, the check is done by parameters into TIWServerController.IWServerControllerBaseGetMainForm
      t:=t+ ' <h4><a href="start/?aparam=secondform&nomemed=XXXX&nm=1>MAMAMA"</a></h4>';
      t:=t+ '</div>';
       IWLab.Text:=t; //RawText IWLabel to my template form rendering
end;


unit ServerController;
interface
uses
  ....
 
type
  TIWServerController = class(TIWServerControllerBase)
    procedure IWServerControllerBaseNewSession(ASession: TIWApplication);
   ......
  private
    { Private declarations }
  public
    { Public declarations }
 
  end;
 
TPCPSession = class(TIWUserSession)
  public
    isLogged:boolean;
    Username:string;
    Password:string;
  end;
 
  function UserSession: TPCPSession;
  function IWServerController: TIWServerController;
 
implementation
 
{$R *.dfm}
 
uses
  IWInit....;
 
function IWServerController: TIWServerController;
begin
  Result := TIWServerController(GServerController);
end;
 
function UserSession: TPCPSession;
begin
  Result := TPCPSession(WebApplication.Data);
end;
 
procedure TIWServerController.IWServerControllerBaseCloseSession(
  aSession: TIWApplication);
var i:integer;
begin
   inc(i); //BREAKPOINT HERE
end;
 
procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);
begin
     TIWMimeTypes.RegisterType('.html', MIME_HTML, True);
     AllowMultipleSessionsPerUser := false;
     ShowStartParams := True;
end;
 
procedure TIWServerController.IWServerControllerBaseGetMainForm(
  var vMainForm: TIWBaseForm);
var FParams : TStringList;
begin
   FParams := TStringList.Create;
    try
      FParams.Text := WebApplication.RunParams.Text;
      if not UserSession.isLogged then begin
         VMainForm:=TIWBaseForm(UserSession.ShowForm(TIWFormLogin));
      end else if (FParams.Count > 1) and (FParams.Values['aparam']='firstform') then begin
          VMainForm := TIWBaseForm(UserSession.ShowForm(TIWFormFirst));
      end else if (FParams.Count >= 1) and (FParams.Values['aparam']='secondform') then begin
         VMainForm:=TIWBaseForm(UserSession.ShowForm(TIWFormSecond));
      end else VMainForm:=TIWBaseForm(UserSession.ShowForm(TIWFormLogin));
    finally
      FParams.free;
    end;
end;
 
procedure TIWServerController.IWServerControllerBaseNewSession(
  ASession: TIWApplication);
var FParams : TStringList;
    s:string;
begin
   FParams := TStringList.Create;
   FParams.text:=ASession.RunParams.text;
   try
     s:=FParams.Values['aparam'];
     if (ASession.RunParams.Count >= 1) and (Lowercase(s)='goout') then  begin
       if UserSession<>nil then
          UserSession.isLogged:=False;
       ASession.TerminateAndRedirect(ASession.ApplicationURL + '/templates/outpage.html'); //enviado ao cliente
     end;
     ASession.Data := TPCPSession.Create(ASession);
   finally
     Fparams.free;
   end;
 
end;
 
initialization
  TIWServerController.SetServerControllerClass;
 
end.


unit UserSessionUnit;
 
interface
 
uses
  IWUserSessionBase, .....
 
type
  TIWUserSession = class(TIWUserSessionBase)
  private
    { Private declarations }
  public
    { Public declarations }
     function ShowForm(AFormClass: TIWAppFormClass):TComponent;
  end;
 
implementation
 
 
{$R *.dfm}
 
function TIWUserSession.ShowForm(AFormClass: TIWAppFormClass):TComponent;
var
  myFormInstance: TComponent;
begin
  myFormInstance :=  WebApplication.FindFormByClassName(AFormClass.ClassName);
  if not Assigned(myFormInstance) then begin
    myFormInstance := AFormClass.Create(WebApplication);
  end;
  TIWAppForm(myFormInstance).Show;
  result:=myFormInstance;
end;
...


Edited by: Luiz Oliveira on Jan 6, 2017 9:39 PM

Edited by: Luiz Oliveira on Jan 7, 2017 12:21 PM
Dan Barclay

Posts: 889
Registered: 11/9/03
Re: Session closing after click on href link to load new page
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 6, 2017 10:05 PM   in response to: Guest in response to: Guest
Luiz Oliveira wrote:
Hi, I have a IWLoginForm, IWFormFirst and IWFormSecond. I use template forms in all my application.

After success login, I save some informations(username, passw, islogged) into Usersession and then I load IWFormFirst.

IWFormFirst has a link as:

   <h4><a href="start/?aparam=secondform&nomemed=XXXX&nm=1>MAMAMA"</a></h4>


to load IWFormSecond.

The problem is that when I click this link the current session is closed and a new Session is triggered. Then I lost all my informations in Usersession.
I put a breakpoint in OnCloseSession and I can see it.

First, I don´t understand why my current session is closed and a new session is opened when I click that link.
Second, I create a class TPCPSession = class(TIWUserSession) to save the data of the current user logged into session. Is it correct to do it?

You will find that your first session is not closed, it's just that the link you provided opens a second session in the browser. The first session will exist until it times out, but you don't see it.

In order to open a new form you need to let Intrweb open (Create) it in the application, opening like a "web page" with a link is not the answer. You should place an IW control on the first page, then in a click event for that control you would start the second form. I just did a quick search of the form for code to do that, this thread may be helpful:

https://forums.embarcadero.com/thread.jspa?messageID=685102&#685102

You can also search for "create form" or "SetActiveForm" on the forum or on atozed.com.

The bottom line for this issue, and some of the other things you're running into, is that you will want to start treating your code as a web application rather than a web site. You need to let Intraweb do most of the work.

Dan
Guest
Re: Session closing after click on href link to load new page
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2017 3:08 AM   in response to: Dan Barclay in response to: Dan Barclay
Dan,

You will find that your first session is not closed, it's just that the link you provided opens a second session in the browser. The first session will exist until it times out, but you don't see it.

I put a breakpoint on event closesession and it´s called when I click on href link.
But you said my session continues active.
In this case, how could I make Intraweb use that session instead of open another session, without make a callback from href link to form?

I have AllowMultipleSessionsPerUser := false; in servercontroller. Is it not enough to prevent open a new session?

I understand what you said, but is there a way to have Intraweb doesn´t open a newsession when creating clickable links on the fly. It´s very common we have these cases when using JS com intraweb..

For sample, when we have a database table and we need loop in your content to show a link to each row, I need to do:
while  not table.eof do
begin
       t:=t+ '<div">';
      t:=t+ ' <h4><a href="start/?aparam=userdados&nomemed='+table.FieldByName('nome').AsString+'&nm='+table.FieldByName('indice').AsString+'">'+table.FieldByName('nome').AsString+'</a></h4>';
      t:=t+ '</div>';
      table.Next;
  end
   //fill  IWLable to use into a template with IWTemplateProcessorHTML
   IWLabel.Text:=t;
end


if the table is big (1000 rows) I´d need many IWlabels in my form. How you could do it whthout to use a link?

Edited by: Luiz Oliveira on Jan 7, 2017 3:19 AM
Guest
Re: Session closing after click on href link to load new page
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2017 12:17 PM   in response to: Guest in response to: Guest
Luiz Oliveira wrote:
Dan,

You will find that your first session is not closed, it's just that the link you provided opens a second session in the browser. The first session will exist until it times out, but you don't see it.

I put a breakpoint on event closesession and it´s called when I click on href link.
But you said my session continues active.
In this case, how could I make Intraweb use that session instead of open another session, without make a callback from href link to form?

I have AllowMultipleSessionsPerUser := false; in servercontroller. Is it not enough to prevent open a new session?

I understand what you said, but is there a way to have Intraweb doesn´t open a newsession when creating clickable links on the fly. It´s very common we have these cases when using JS com intraweb..

For sample, when we have a database table and we need loop in your content to show a link to each row, I need to do:
while  not table.eof do
begin
       t:=t+ '<div">';
      t:=t+ ' <h4><a href="start/?aparam=userdados&nomemed='+table.FieldByName('nome').AsString+'&nm='+table.FieldByName('indice').AsString+'">'+table.FieldByName('nome').AsString+'</a></h4>';
      t:=t+ '</div>';
      table.Next;
  end
   //fill  IWLable to use into a template with IWTemplateProcessorHTML
   IWLabel.Text:=t;
end


if the table is big (1000 rows) I´d need many IWlabels in my form. How you could do it whthout to use a link?

Dan,

I have created a JS callback from href link of my html page template. The template is rendering in my IWForm.
It captures the anchor href and trigger OnMedicoClick in my FirstForm passing the parameters, as well

Here is the JS:
 $( document ).ready( function() {
			 $("div h4 a").click(function() {
			    param=$(this).attr('href');
  		            executeAjaxEvent("&data="+param, null,"OnMedicoClick",false, null, false);
		            return false;
		     });
  });


I have read in other post from Alexandre that if I use GotoUrl I can redirect without destroy the session( https://forums.embarcadero.com/thread.jspa?messageID=832227󋋣 ).
But, the problem is that the ServerController continues creating a new session.
And, the data of my current user is lost because It was in the old session.
I need to preserve the login and password from user in the session.
If a new session is created, I have a problem.

Do you know how can I avoid that ServerController create a new session when clicking in the href link to a new form?

With this code, the event OnMedicoClick is trigged, but It continues opening new session when I use GotoUrl.

I need to open the Second Form without trigger onNewSession of Seerver Controller

As you said, I must let IW do its work, but it is not cooperating with me.

//This event is triggered from FirstForm and It needs open the SecondForm without create a new session
//The JS code in the FirstForm make it run
procedure TIWFormMedicos.OnMedicoClick(EventParams: TStringList);
var data,nomemed, nm:string;
    FParams:TStringList;
begin
    data:=EventParams.Values['data']; //start/?aparam=userdados
    nomemed:=EventParams.Values['nomemed'];//nomemed=John
    nm:=EventParams.Values['nm'];//nm=Doe
    s:=format('%s&nomemed=%s&nm=%s',[data,nomemed,nm]);
    WebApplication.GoToURL(WebApplication.ApplicationURL+'/$/'+s); 
end;
 


Edited by: Luiz Oliveira on Jan 7, 2017 12:20 PM
Dan Barclay

Posts: 889
Registered: 11/9/03
Re: Session closing after click on href link to load new page
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2017 1:25 PM   in response to: Guest in response to: Guest
Luiz Oliveira wrote:
I have AllowMultipleSessionsPerUser := false; in servercontroller. Is it not enough to prevent open a new session?

I understand what you said, but is there a way to have Intraweb doesn´t open a newsession when creating clickable links on the fly. It´s very common we have these cases when using JS com intraweb..

OK. I have not used js callback so can't help with details on that part. However, I can tell you that the command you used:

   <h4><a href="start/?aparam=secondform&nomemed=XXXX&nm=1>MAMAMA"</a></h4>


will start a new session.

In my current work I'm using AllowMultiple... true so I don't recall the url text to communicate with the session, but you can be certain it does not contain the keyword "start".

Try simply removing that keyword, such as

   <h4><a href="/?aparam=secondform&nomemed=XXXX&nm=1>MAMAMA"</a></h4>

Dan

Guest
Re: Session closing after click on href link to load new page
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2017 3:07 PM   in response to: Dan Barclay in response to: Dan Barclay
Dan Barclay wrote:
Luiz Oliveira wrote:
I have AllowMultipleSessionsPerUser := false; in servercontroller. Is it not enough to prevent open a new session?

I understand what you said, but is there a way to have Intraweb doesn´t open a newsession when creating clickable links on the fly. It´s very common we have these cases when using JS com intraweb..

OK. I have not used js callback so can't help with details on that part. However, I can tell you that the command you used:

   <h4><a href="start/?aparam=secondform&nomemed=XXXX&nm=1>MAMAMA"</a></h4>


will start a new session.

In my current work I'm using AllowMultiple... true so I don't recall the url text to communicate with the session, but you can be certain it does not contain the keyword "start".

Try simply removing that keyword, such as

   <h4><a href="/?aparam=secondform&nomemed=XXXX&nm=1>MAMAMA"</a></h4>

Dan


Dan Thanks, but It doesn´t work.

Maybe, Daniel or Alexandre can help me, because they have more expertise in Intraweb than me.

I am stuck with this problem since three days.

But, I am glad you are trying help me.

Edited by: Luiz Oliveira on Jan 7, 2017 3:45 PM
Dan Barclay

Posts: 889
Registered: 11/9/03
Re: Session closing after click on href link to load new page
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2017 4:51 PM   in response to: Guest in response to: Guest
Luiz Oliveira wrote:
Dan Barclay wrote:
Luiz Oliveira wrote:
I have AllowMultipleSessionsPerUser := false; in servercontroller. Is it not enough to prevent open a new session?

I understand what you said, but is there a way to have Intraweb doesn´t open a newsession when creating clickable links on the fly. It´s very common we have these cases when using JS com intraweb..

OK. I have not used js callback so can't help with details on that part. However, I can tell you that the command you used:

   <h4><a href="start/?aparam=secondform&nomemed=XXXX&nm=1>MAMAMA"</a></h4>


will start a new session.

In my current work I'm using AllowMultiple... true so I don't recall the url text to communicate with the session, but you can be certain it does not contain the keyword "start".

Try simply removing that keyword, such as

   <h4><a href="/?aparam=secondform&nomemed=XXXX&nm=1>MAMAMA"</a></h4>

Dan


Dan Thanks, but It doesn´t work.

Maybe, Daniel or Alexandre can help me, because they have more expertise in Intraweb than me.

I am stuck with this problem since three days.

But, I am glad you are trying help me.

Yes, they know far more than I, particularly on this subject. I learn by trying to help (though I do point out when I'm not sure about something). I have tried to stick to pure IW code in my on work, just as a maintenance precaution, so callbacks aren't something I've spent time on. It's also why I preach so much to let IW do the work Yes, I know I say that too much!<g>

While you are waiting on their input, here are a couple of additional thoughts:

1. If you are not already doing so, use Fiddler to see how Intraweb itself communicates back to the application. You might see an example of what it's supposed to look like.

2. Use Intraweb code to do the callback. There are javascript functions that IW uses for this. Look for "executeAjaxEvent()". In fact, searching just now I found these links with examples that may be helpful:

http://www.devsuperpage.com/search/Articles.aspx?G=2&ArtID=104720
http://www.atozed.com/intraweb/docs/Develop/Ajax/AsyncEventHandling.aspx
http://alexandrecmachado.blogspot.com/2012/03/show-loading-animation-during-long.html

[edit] There is also an example in the Intranet Sample code project named "CustomAjaxCall"

Dan

Edited by: Dan Barclay on Jan 7, 2017 7:24 PM
Dan Barclay

Posts: 889
Registered: 11/9/03
Re: Session closing after click on href link to load new page
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2017 5:53 PM   in response to: Dan Barclay in response to: Dan Barclay
You are probably already using the demos from IW, but for others not doing that here is where they are:

Codeplex for IW: http://iwdemos.codeplex.com/

Shortform directions to get zip containing all demos:
1. Go to the Codeplex web page ( http://iwdemos.codeplex.com/ ).
2. Click the "Source Code" tab.
3. ON THAT TAB, click "Download" (not the Download *tab*)

You can also link to the Codeplex projects with Subversion.
I have that as a subversion project on my drive to make it easier to follow changes.

Dan
Alexandre Machado

Posts: 1,754
Registered: 8/10/13
Re: Session closing after click on href link to load new page
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2017 6:50 PM   in response to: Guest in response to: Guest
The problem is that when I click this link the current session is closed and a new Session is triggered.

Do you have ServerController.AllowMultipleSessionsPerUser = True? If not, this is the expected behavior. Forcing a new session to start in the same browser (using /$/Start in the URL) will terminate your previous session and start a new one.This is by design.

Think about it: You are explicitly saying "I don't want the user to have 2 sessions" when you set AllowMultipleSessionsPerUser to false. Then you try to create a new session for the same user/browser. What IntraWeb should do? It does exactly what you are commanding, implicitly: "Close my previous session and start a new one for me (once I'm explicitly forbidding the user to have more than one active session)".

I think you don't need the "/start" part in your URL. Just remove it. The IntraWeb session has been already created. You just need to give the user full access to it and show the form.
Guest
Re: Session closing after click on href link to load new page
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 8, 2017 4:07 AM   in response to: Alexandre Machado in response to: Alexandre Machado
Alexandre Machado wrote:
The problem is that when I click this link the current session is closed and a new Session is triggered.

Do you have ServerController.AllowMultipleSessionsPerUser = True? If not, this is the expected behavior. Forcing a new session to start in the same browser (using /$/Start in the URL) will terminate your previous session and start a new one.This is by design.

Think about it: You are explicitly saying "I don't want the user to have 2 sessions" when you set AllowMultipleSessionsPerUser to false. Then you try to create a new session for the same user/browser. What IntraWeb should do? It does exactly what you are commanding, implicitly: "Close my previous session and start a new one for me (once I'm explicitly forbidding the user to have more than one active session)".

I think you don't need the "/start" part in your URL. Just remove it. The IntraWeb session has been already created. You just need to give the user full access to it and show the form.

Alexandre,

It works very well. Thank you.

But I´d like to ask:.

1. If I remove /start, the correct event to handle what page will be load (by check my url parameters), always will be OnBeforeRender of ServerController? I ask you it because GetMainForm is no more called.

2. I have a login screen for users. On login, I save user login, password and other fields into Usersession derived class. If I change my app enabling AllowMultipleSessionsPerUser=true, how do I preserve these info to be available in all web app forms?.
Alexandre Machado

Posts: 1,754
Registered: 8/10/13
Re: Session closing after click on href link to load new page
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 8, 2017 2:05 PM   in response to: Guest in response to: Guest
1. If I remove /start, the correct event to handle what page will be load (by check my url parameters), always will be OnBeforeRender of ServerController? I ask you it because GetMainForm is no more called.

2. I have a login screen for users. On login, I save user login, password and other fields into Usersession derived class. If I change my app enabling AllowMultipleSessionsPerUser=true, how do I preserve these info to be available in all web app forms?

What exactly are you trying to accomplish? I think that you need to register 2 different Content Handlers, one for each URL (without /$/Start). Then you can have /YourStartUrl1 redirecting to one content handler (which will create/show one IWForm) and another /YourStartUrl2 redirecting to another content handler.
Guest
Re: Session closing after click on href link to load new page
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 10, 2017 6:28 AM   in response to: Alexandre Machado in response to: Alexandre Machado
Alexandre Machado wrote:
1. If I remove /start, the correct event to handle what page will be load (by check my url parameters), always will be OnBeforeRender of ServerController? I ask you it because GetMainForm is no more called.

2. I have a login screen for users. On login, I save user login, password and other fields into Usersession derived class. If I change my app enabling AllowMultipleSessionsPerUser=true, how do I preserve these info to be available in all web app forms?

What exactly are you trying to accomplish? I think that you need to register 2 different Content Handlers, one for each URL (without /$/Start). Then you can have /YourStartUrl1 redirecting to one content handler (which will create/show one IWForm) and another /YourStartUrl2 redirecting to another content handler.

Thank you Alexandre. I am going trying and I will post my results.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02