Thursday, February 21, 2008

How To Keep Session Live Till The Page Stays Open


One one of my project i was told that the user will keep the browser open and will
talk to their client for long time, which may be 1/2 hour continuous and after that
they will come back and add some item to the page as per the discussion over the
phone. So we have to keep the session live till that period to keep him logged in.
The way to implement this are


1) Add the following code in web.config file

<sessionState timeout="100"></sessionState>Here timeout is in munites and sessionwill stay live upto 180 min i.e 3 hours
(by default the timeout is 20 mins) which was enough for our purpose.


But with this way of implementation we were misusing the valuable server disk space
for all the session variables will stay for that long time. So even if you close
the browser the objects will stay in the server for next 3 Hours. So we implemented
another way


2) In the Body tag of our aspx page i put a div which is having display style attribute='None'
so that the div does not display in the page.


And then put a IFrame inside the div and set another aspx page (KeepSession.aspx)
as the src of the IFrame. The the page in the IFrame i make it auto refresh after
18 mins. In this way though the session is to be expired in 20 mins (by default),
but the KeepSession.aspx page will be reloaded in every 18 mins and the session
will be refreshed and it will start counting the 20 mins expire time from start.
So in this way it will stay alive for ever. Below in the quote in my NewQuote.aspx


::::::::::::::::::::


<div style="display:none">

<iframe id="redresh" src="RefreshSession.aspx" height="0" width="0">

</iframe>

</div>

<form id="form1" runat="server">

<div>YOUR PAGE CODE HERE </div>

</form>

</body>........

KeepSession.aspx Page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="KeepSession.aspx.cs"
Inherits="KeepSession" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Refresh Session</title>

<meta http-equiv="refresh" content="1048" />
<!--Here the content value is the no of secconds
after which the page will be reloaded automatically. Make it less than the session
expire time -->

</
head>

<body>

<form id="form1" runat="server

form>

</body>

</html>

3 comments:

escu said...

Is this working? Have you tested it?

Thank you,
P

Pierz Newton-John said...

Doesn't work, as far as I can tell. I've implemented exactly as written, and the session continues to relentlessly expire.

Tarun Ghosh said...

I just chekced this again. The same was required in a situation for Form Authentication. This is working for Form authetication too.
Please make sure you place the code in properly in the RefreshSession.aspx page.