Asp.net Extend session

Hi. This is Satalaj and today I'm going to sahre the trick of extending session using ASP.net and JavaScript.

Here is a scenario.
  Most of the ecommerce site carry forwards the user data from first page to second page. on actual order page they process order.
In order page they write attractive stories where user may require more than 20 to 30 min. to read and some time to get the CC and fill 
it in. So if session gets expired on order page user gets redirected to index page or landing page. 
How to avoid this?  lets see.

 Create  one page in same application name it as SessionExtender.aspx
write below code on page load event of it

protected void Page_Load(object sender, EventArgs e)

{

Session.Timeout = (!

string.IsNullOrEmpty(Request.QueryString["t"])) ? (Int16)Request.QueryString["t"] : 10;

// here we are passing time to extend values in query string if it is null it will take 10 min


Response.End();

}

Now open othe pages like home page.aspx etc where you would like to extend the session.

in body tag call below java script function

ExtendSession() some thing like this

<body onload="ExtendSession();">

</body>

In your head tag add below javascript

<script>
function ExtendSession()()
{
var img = new Image();
img.src="SessionExtender.aspx";   // or using query string t as img.src="SessionExtender.aspx?t=5";  
alert('called'); // make sure it is called after 3 second you can delete it. It is just for your refrence or **
}

function ExtendSession()
{
window.setInterval("ExtendSession()",3000);
}
</script>

** To know wheather your page SessionExtender he getting called after every 3 seconds
Please install HttpWatch basic or fidler

This scenario will help you extend session based at runtime based on pick load on your web or heavy traffic site

Thanks, Satalaj.
Download true story of internet http://www.revenmerchantservices.com/freewatchmovie/download_truestory_internet.aspx

By Sat Sat   Popularity  (2210 Views)