473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

How to extend Session timeout

Hi,

I am creating a session when user successfully login to the site and user gets a form to input some data into database.

Assume that user entered data into some fileds and let the browser ideal and doing some other work in his/her system.

My requrement is, 2 mins before(after 13 mins) of session timeout(I set session timeout as 15 mins in web.config), user should get an alert as" Your session is about to timeout in 2 mins(at 12:30:20PM), Do you want to extend session timeout?" with OK and Cancel button.

If user clicked on OK button with in session timeout period(within 15th minute), session should expire to one more 15 minutes, but browser should not refresh.(If browser refreshed, all the values entered by the user, will be cleared.)

If user didnot clicked any button with in session timeout or user clicked Cancel button or user clicked on OK button after session timout, session should expire at 15th minute.



Thanks,

Anitha.
Aug 18 '09 #1
7 20824
Frinavale
9,735 Expert Mod 8TB
You can do this using Ajax and JavaScript.

When the page is sent down to the browser the JavaScript should start tracking how much time has passed. When the user has 2 minutes left the JavaScript should display the message.

If the user clicks ok then the JavaScript should make an Ajax Call to the server....it should call a method in the page. When this happens a partial page update will occur (which you can keep hidden) which will reset the time automatically.

The session only times out after 15 minutes of inactivity...therefore if you have any activity at all (simply calling a method on the server will suffice) then the Session will be reset.
Aug 18 '09 #2
Can you give an example on how to do it.
It would be great if it is in VB.
Thanks,
Sep 30 '09 #3
Frinavale
9,735 Expert Mod 8TB
You can't use VB.NET to do this....

It would probably be easiest if you used DOM objects to do this....by this I mean stay away from the built in JavaScript "confirm" etc.

This is what I suggest you do (please note that I'm assuming that the session expires in 2 minutes and I am displaying the message 5 seconds before the session expires...you have to modify the code to suit your own needs)
  • Place a Panel on the page. You will display this panel using JavaScript after, say 115 seconds has past, giving the user 5 seconds to respond
  • Place a Label in the Panel and set the Text property to prompt the user. Have it say something like "Your session is going to expire in 5 seconds. Would you like to extend it?".
  • Place an Ok button in the Panel. Set it's text property to "yes". When the user clicks this button it will post back to the server which will extend the Session life.
  • Place an HTML input type=button in the Panel next to the Ok button. Set the value of the HTML button to "no". This button will be used to "hide" the Panel if the user clicks "no". It will not postback to the server and so the Session life will not be extended in this case.
  • Implement a JavaScript method that displays the Panel in the middle of the page. This will be called after 115 seconds has passed using the JavaScript setTimeout() method.
  • Implement a JavaScript method that will hide the Panel. This method will be called when the user clicks the "no" button. This method will also be called when after 2 minutes (120 seconds) has passed to close the window if the user hasn't selected to extend the Session life.

So your ASPX page is going to have something like (please take note of the style that I have set for the Panel):
Expand|Select|Wrap|Line Numbers
  1. <asp:Panel ID="ExtendSessionLifeBox" runat="server" style="height:100px; width:250px; background-color:#B0E2FF; border:solid 1px black; display:none; position:absolute;">
  2.   <asp:Label ID="ExtendSessionLifePrompt" runat="server" Text="Your session is going to expire in 5 seconds. Would you like to extend your Session for another 2 minutes?"></asp:Label>
  3.   <asp:Button ID="ExtendSessionLife" runat="server" Text="Yes" />
  4.   <input type="button" id="CancelExtendSessionLife" value="No" onclick="CloseExtendSessionLifeBox(); return false;" />  
  5. </asp:Panel>
Notice how the the "no" button calls a JavaScript method named CloseExtendSessionLifeBox(). You have to implement that method. This method needs to retrieve the ExtendSessionLifeBox element on the page and set it's style so that it's not displayed (use dispaly:none to hide it).

You can retrieve the element using the JavaScript document.getElementByID() method. The only thing is that you have to know the ID of the element. The Panel's ID may not match the ID that is required by the document.getElementByID() method.

When an ASP.NET Panel is rendered as HTML it is rendered as a <div> element. This <div> element is given an ID but it may not match the ID of the ASP.NET Panel. The reason is because you may be using MasterPages which can display more than one "aspx" page in one page (or you may be using User Controls). So, in theory, each aspx page (or each User Control) in the MasterPage (or aspx page) can have a Panel with the id "ExtendSessionLifeBox". In order to differentiate between these Panels ASP.NET changes the ID of the <div>s that represent the Panels so that each <div> has a unique ID in the browser.

You can access the ID that the <div> associated with the Panel using the Panel's ClientID property. This property will return the ID of the <div> element.

So, you need to use the ClientID of the Panel in the JavaScript function that "hides" the Panel asking the user if they want to extend the Session life.

You can write the Panel's ClientID value into the JavaScript function using the Response.Write() method. The Response.Write method should never be used in your VB.NET code because it will print the value in some random place in the page. Usually this results in the output being printed before the <html> element which makes the page invalid. However, you can use the Response.Write() method in your asp code because you can specify exactly where the stuff should be printed.

To do this you would use the ASP tags <% %>. These tags specify that whatever is in between them should be executed on the server. This means that whatever is in between these tags should be VB.NET code (because you're using VB.NET...but if someone using C# were trying to do the same thing as you, then whatever is in between these tags should be C# code).

So, to print the ClientID of the Panel into the page you would use:
Expand|Select|Wrap|Line Numbers
  1. <% Response.Write(ExtendSessionLifeBox.ClientID) %>
An asp short hand version of Response.Write is <%= %>, so in my code I use:
Expand|Select|Wrap|Line Numbers
  1. <%= ExtendSessionLifeBox.ClientID %>
Ok, now that we have the basics covered I'll show you what the CloseExtendSessionLifeBox() JavaScript method should look like:

Expand|Select|Wrap|Line Numbers
  1. function CloseExtendSessionLifeBox(){
  2.   var boxElement = document.getElementById("<%= ExtendSessionLifeBox.ClientID %>");
  3.  
  4.   //checking if the element exists before trying to use it
  5.   if(boxElement)
  6.   {
  7.     boxElement.style.display = "none";
  8.   }
  9. }
Wonderful! Now we have a method that hides the box....but that's not really going to help us because by default I have specified that the box has a style of display:none (so it's not showing by default). You use the same technique to display the box only instead of setting the style of the box to display:none you set it to display:block:

The following method display the ExtendSessionLifeBox:

Expand|Select|Wrap|Line Numbers
  1. function DisplayExtendSessionLifeBox(){
  2.   var boxElement = document.getElementById("<%= ExtendSessionLifeBox.ClientID %>");
  3.  
  4.   //checking if the element exists before trying to use it
  5.   if(boxElement)
  6.   {
  7.     boxElement.style.display = "block";
  8.   }
  9. }

Please note that I have set the ExtendSessionLifeBox's style to have a position:absoulte. This means that it is going to "float on top of" the rest of the page content.

The thing is that it's going to appear in the top left corner of page....but you probably want this to display in the middle of the page instead.

To do this you need to use JavaScript to determine the height and width of the browser, and the height and width of the ExtendSessionLifeBox so that you can center the ExtendSessionLifeBox in the middle of the browser window.

You need to take the height of the browser divided by 2 and subtract the height of the ExtendSessionLifeBox divided by 2 to determine how far down from the top the ExtendSessionLifeBox should be displayed. Likewise, you would take the width of the browser divided by 2 and subtract the width of the ExtendSessionLifeBox divided by 2 to determine how far from the left the ExtendSessionLifeBox should be displayed.

Once you've figured out how far from the top and how far from the left to display the ExtendSessionLifeBox you need to set the ExtendSessionLifeBox's top and left styles.

Please note that you can only figure out how tall and wide the ExtendSessionLifeBox is if the ExtendSessionLifeBox is being displayed (if it has a style of display:none then these values are 0).

Here is the code that will display the ExtendSessionLifeBox in the middle of the page:

Expand|Select|Wrap|Line Numbers
  1. function BrowserWidth() {
  2.     var theWidth;
  3.     if (window.innerWidth) {
  4.         theWidth = window.innerWidth;
  5.     }
  6.     else if (document.documentElement && document.documentElement.clientWidth) {
  7.         theWidth = document.documentElement.clientWidth;
  8.     }
  9.     else if (document.body) {
  10.         theWidth = document.body.clientWidth;
  11.     }
  12.     return theWidth;
  13. }
  14. function BrowserHeight() {
  15.     var theHeight;
  16.     if (window.innerHeight) {
  17.         theHeight = window.innerHeight;
  18.     }
  19.     else if (document.documentElement && document.documentElement.clientHeight) {
  20.         theHeight = document.documentElement.clientHeight;
  21.     }
  22.     else if (document.body) {
  23.         theHeight = document.body.clientHeight;
  24.     }
  25.    return theHeight;
  26. }
  27.  
  28. function DisplayExtendSessionLifeBox(){
  29.  
  30.   var boxElement = document.getElementById("<%= ExtendSessionLifeBox.ClientID %>");
  31.   if(boxElement){
  32.  
  33.     boxElement.style.display="block";
  34.     var bw = BrowserWidth();
  35.     var bh = BrowserHeight();
  36.     var boxElementWidth = (boxElement.clientWidth)? boxElement.clientWidth : boxElement.offsetWidth;
  37.     var boxElementHeight = (boxElement.clientHeight)? boxElement.clientHeight: boxElement.offsetHeight;
  38.  
  39.     var boxElementTop = (bh/2) - (boxElementHeight/2);
  40.     var boxElementLeft = (bw/2) - (boxElementWidth/2);
  41.     boxElement.style.top = boxElementTop;
  42.     boxElement.style.left = boxElementLeft;
  43.     boxElement.style.position="absolute";
  44.   }
  45. }
Ok, so now we have a method that displays the ExtendSessionLifeBox in the middle of the web browser. The ExtendSessionLifeBox informs the user that their Session is about to expire and asks them if they want to extend it for another 2 minutes. The ExtendSessionLifeBox has 2 buttons: an "ok" button that will submit the page to the server, thereby extending the Session life; and a "close" button that simply closes the ExtendSessionLifeBox.

All we have to do now is call the method that displays the ExtendSessionLifeBox after 115 seconds (5 seconds before the session expires) and call the method that hides the ExtendSessionLifeBox after 2 minutes (120 seconds) because the Session has already expired at that point.

To do this you use the JavaScript setTimeout() method. The setTimeout method will execute a method after a set amount of time has passed. It takes 2 parameters, the name of the method to call, and the amount of time to wait before calling the method (in milliseconds).

The following code will call the DisplayExtendSessionLifeBox() method after 115 seconds, and will call the HideExtendSessionLifeBox() method after 2 minutes:
Expand|Select|Wrap|Line Numbers
  1. setTimeout(DisplayExtendSessionLifeBox,115000);
  2. setTimeout(CloseExtendSessionLifeBox,120000);
Now you should have enough information to solve your problem. Normally I don't spoon feed the answer to people but this time I didn't feel like wasting time on you trying something, me responding, you trying something else, and me responding...

Please note that all JavaScript should always be in <script type="text/javascript"></script> tags.

Happy coding,

-Frinny
Oct 1 '09 #4
ssnaik84
149 100+
@Frinny..
how can you give such long reply?? amazing..!!!
keep it up buddy.. :)
Oct 1 '09 #5
Frinavale
9,735 Expert Mod 8TB
Hehe like I said I don't normally like to "spoon feed" answers. I like it when people take their own initiative and learn for themselves...because when the come back here with questions later they are more likely to have a more interesting question that I might actually learn from :)

But this time I didn't feel like spending several days going back and forth with the OP over the course of days when it could take me less time to just type the answer out.



-Frinny
Oct 2 '09 #6
Hi Frinavale,

Many thanks for your solution. It works only when the session time out period is less than time to close the warning panel(window).

e.g.
Expand|Select|Wrap|Line Numbers
  1. setTimeout(DisplayExtendSessionLifeBox,115000); //display after 115 seconds
  2. setTimeout(CloseExtendSessionLifeBox,120000);//Close after 120 seconds
  3.  
but
if we have
Expand|Select|Wrap|Line Numbers
  1. setTimeout(DisplayExtendSessionLifeBox,120000);//display after 120 seconds
  2. setTimeout(CloseExtendSessionLifeBox,1150000);//Close adter 115 seconds
  3.  
It does not hide the warning window in the second scenario. Pretty simple because "CloseExtendSessionLifeBox()" method is already invoked before warning message box being displayed!

The solution would be to modify your code - calling
CloseExtendSessionLifeBox() infinitely. So adding this statement within the "CloseExtendSessionLifeBox()" will resolve the problem.
....
Expand|Select|Wrap|Line Numbers
  1. function CloseExtendSessionLifeBox(){
  2.   var boxElement = document.getElementById("<%= ExtendSessionLifeBox.ClientID %>");
  3.  
  4.   //checking if the element exists before trying to use it
  5.   if(boxElement)
  6.   {
  7.     boxElement.style.display = "none";
  8.   }
  9.   setTimeout(CloseExtendSessionLifeBox,1150000);
  10. }
  11.  
Thanks a lot again for your cool solution.

Cheers,
Milan Gurung
Oct 19 '10 #7
Frinavale
9,735 Expert Mod 8TB
Thanks for the info Milan Gurung :)

I just read over what I wrote... I must have had time on my hands.

I have edited my original post to include your code in the CloseExtendSessionLifeBox() method.

-Frinny
Oct 19 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jenna | last post by:
Hi all, Do you know how to reset session.timeout after user click to extend the session timeout? For example, default session.timeout is 20 mins and 2 mins before will ask whether extend...
1
by: J. Muenchbourg | last post by:
I have a session object Session("login") whereas I need to extend the time on this to 1 hour, instead of the default 20 minutes. After initializing Session("login"), how would I change the timeout...
4
by: HolaGoogle | last post by:
hi there, i've 2 questions for you guys.... 1: is there any way to "force" a session_onend(), session timeout or at least call my logout method when a user leaves the application window without...
4
by: Yuriy | last post by:
Hi, everybody! I need every 20 min extent my Session timeout. Any suggetions appreciated! Thanks
0
by: Chakra Venkatesan | last post by:
Hi, My application has a session timeout of 15 mins. I have a page for which I have disabled the session state. If I invoke the this page from a new browser session, there is no session created...
2
by: Yuriy | last post by:
Hello ! I have a problem with session variables and i need to do something with my session I need every 20 min extend my Session timeout. Any suggetions appreciated! Thanks
3
by: kurt | last post by:
Hi All. I have a page that users enter information in text boxes...and sometimes it's a lot of information. There are times that these folks either take their time entering the info or do other...
5
by: thefox149 | last post by:
Hi All I know I can set the php.ini timeout to increase to what ever I like I also know I can do it from a php script. My question is if I can change it from the script will it extend the...
1
by: =?Utf-8?B?VGVycnkgSG9sbGFuZA==?= | last post by:
I work for a organisation that has a corporate IT dept who have set session timeout to be 20 mins on the web server and we as developers have no facility to change or influence this. We have a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.