Alternative to Session_End

J

Jared

Hi

I am using StateServer as my sessionState mode. I realise that I cannot
use the Session_End in global.asax. I just want to do some simple
logging of site usage.

Has anybody got a simple solution that replaces the Session_End object?

I was thinking of using client-side script to redirect to another page.
I can then use this page to enter the information I require into the
database. Is there any script that will see the Page Unload event? I
realise that this may not function in all browsers, but the site is
currently only intranet based, so it's not an issue right now.

Thanks

Jared
 
P

Patrice

You could just have a logout page and perfom your processing here. Keep in
mind though that the user might well just close directly the browser (you
could likely still use client side script events and call a server side
page).

What is the exact information you want ?

You could work with HTTP rather than against and base the end of the session
(if this is what you are after) on the last HTTP request the server gets
rather than on the actual time where the user closed his browser (he could
go to lunch and close it or forget it under other windows and close it, how
valuable it is to get the time he closed the window compared with the time
he lasked ask something to the server ?) You can eventually add the default
timeout so that you are lined up with the technical end of the session...
 
J

Jared

Patrice

Thanks for your quick response.

I want the user to be able to close the browser to log-off. I already
have a logoff button, but do not want to force them to use that, unless
I can with client-side script.

How would I record the user asking something? Would I use
Application_EndRequest?

Many Thanks

Jared
 
J

Jared

For any body that's interested I have found a solution to this problem
that works for me.

What I do is use javascript to detect whether the close button has been
clicked on the browser and launch a pop-up window, which populates my
database and ends my session if it has. The basic code is here;


//This Code goes in the page_load event for each page in the
application. This code is just registering the javascript.

if(!this.IsClientScriptBlockRegistered("clientScript"))
this.Page.RegisterClientScriptBlock("clientScript",strScript);


//This code goes in the Body html tag of each page in the application

<Body onunload="logoff('logout.aspx')">


//This is the javascript to check for the browser close button and open
a log-off
//pop-up window (strScript in the example above).

//You can type this javascript directly into the html of each page and
it has
//the same effect, but there is less manipulation involved if you store
it in a class.

//The important bit is "if (event.clientY < 0)", becuase this
determines whether or not
//the close (x) button has been clicked, by looking at the mouse
pointer location.


<script language=javascript>
<!--
var newwindow = ''
function logoff(url)
{
if (event.clientY < 0)
{
if (!newwindow.closed && newwindow.location)
{
newwindow.location.href = url;
}
else
{
newwindow=window.open(url,'Logout','height=150,width=100');
if (!newwindow.opener) newwindow.opener = self;
}
}
if (window.focus)
{
newwindow.focus()
}
return false;
}
// -->
</script>


//This is the code on the Page_Load event of logoff.aspx. In this
example I
//use Session["strUserID"] to determine whether or
//not the user has logged in to the web-site, as this is optional. The
pop-up window
//closes automatically if the user has not logged in.
//I have a call to a function which populates my database log info here
first, then;

if((string)Session["strUserID"] == "")
{
Page.RegisterStartupScript("CloseScript","<script language=javascript>
window.close(); </script>");
}
else
{
lblMessage.Text = "<p align=center>Brands Manual<br><br>Logout
Successful<br><br><a
href='javascript:window.close()'>Close</a></font>";
}
Session.Abandon();
 
J

Jared

Try adding expanding the javascript function to include
'event.clientX'. I noticed in the example below that when the
navigation buttons were clicked the pop-up was displayed. Adding this
extra bit seems to cure the issue.

if (event.clientY < 0 && event.clientX <0)
 
K

KMA

I can see that it's possible to detect when the close button is clicked. But
what if the user uses AltF4 to close the browser? Wouldn't you favour a
server side solution?


Jared said:
Try adding expanding the javascript function to include
'event.clientX'. I noticed in the example below that when the
navigation buttons were clicked the pop-up was displayed. Adding this
extra bit seems to cure the issue.

if (event.clientY < 0 && event.clientX <0)

For any body that's interested I have found a solution to this problem
that works for me.

What I do is use javascript to detect whether the close button has been
clicked on the browser and launch a pop-up window, which populates my
database and ends my session if it has. The basic code is here;


//This Code goes in the page_load event for each page in the
application. This code is just registering the javascript.

if(!this.IsClientScriptBlockRegistered("clientScript"))
this.Page.RegisterClientScriptBlock("clientScript",strScript);


//This code goes in the Body html tag of each page in the application

<Body onunload="logoff('logout.aspx')">


//This is the javascript to check for the browser close button and open
a log-off
//pop-up window (strScript in the example above).

//You can type this javascript directly into the html of each page and
it has
//the same effect, but there is less manipulation involved if you store
it in a class.

//The important bit is "if (event.clientY < 0)", becuase this
determines whether or not
//the close (x) button has been clicked, by looking at the mouse
pointer location.


<script language=javascript>
<!--
var newwindow = ''
function logoff(url)
{
if (event.clientY < 0)
{
if (!newwindow.closed && newwindow.location)
{
newwindow.location.href = url;
}
else
{
newwindow=window.open(url,'Logout','height=150,width=100');
if (!newwindow.opener) newwindow.opener = self;
}
}
if (window.focus)
{
newwindow.focus()
}
return false;
}
// -->
</script>


//This is the code on the Page_Load event of logoff.aspx. In this
example I
//use Session["strUserID"] to determine whether or
//not the user has logged in to the web-site, as this is optional. The
pop-up window
//closes automatically if the user has not logged in.
//I have a call to a function which populates my database log info here
first, then;

if((string)Session["strUserID"] == "")
{
Page.RegisterStartupScript("CloseScript","<script language=javascript>
window.close(); </script>");
}
else
{
lblMessage.Text = "<p align=center>Brands Manual<br><br>Logout
Successful<br><br><a
href='javascript:window.close()'>Close</a></font>";
}
Session.Abandon();
 
J

Jared

I see where you are coming from (I am just banking on my users not
using Alt-F4) for now). I would prefer a server-side solution, but it
is not made easy when using StateServer. Do you have a server-side
solution that is simple to implement?

Thanks

Jared
 
J

Jared

Managed to find this javascript that detects Alt-F4 and opens a message
box. Although the cancel routine does not work, the fact that the
message box is modal, means that the pointer co-ordinates from my
previous code work and I get my session ended.

function CheckKeys()
{
function cancel()
{
// local function
event.keyCode = 0;
event.cancelBubble = true;
return false;
}
var allowedBack = "INPUT,TEXTAREA,";
with(event)
{
//alert("keyCode: "+keyCode);
if(altLeft&&keyCode==115)
{
// ALT+F4
alert("ALT+F4"); // this work
return cancel(); // this not working
}
}
}

Jared
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Open-source alternative to mainwp 0
Session_End 14
RE Session_end 2
Global.asax: Session_End() Behavior 12
Session_End event not working 2
Session_End? 2
Session_End Pop Up 1
Session_End Event 7

Staff online

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top