473,748 Members | 4,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to extend Session timeout

11 New Member
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 20943
Frinavale
9,735 Recognized Expert Moderator Expert
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...th erefore 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
ybechar
1 New Member
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 Recognized Expert Moderator Expert
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 CloseExtendSess ionLifeBox(). You have to implement that method. This method needs to retrieve the ExtendSessionLi feBox 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.getEle mentByID() 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.getEle mentByID() 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 "ExtendSessionL ifeBox". 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 CloseExtendSess ionLifeBox() 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 ExtendSessionLi feBox:

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 ExtendSessionLi feBox's style to have a position:absoul te. 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 ExtendSessionLi feBox so that you can center the ExtendSessionLi feBox 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 ExtendSessionLi feBox divided by 2 to determine how far down from the top the ExtendSessionLi feBox should be displayed. Likewise, you would take the width of the browser divided by 2 and subtract the width of the ExtendSessionLi feBox divided by 2 to determine how far from the left the ExtendSessionLi feBox should be displayed.

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

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

Here is the code that will display the ExtendSessionLi feBox 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 ExtendSessionLi feBox in the middle of the web browser. The ExtendSessionLi feBox informs the user that their Session is about to expire and asks them if they want to extend it for another 2 minutes. The ExtendSessionLi feBox 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 ExtendSessionLi feBox.

All we have to do now is call the method that displays the ExtendSessionLi feBox after 115 seconds (5 seconds before the session expires) and call the method that hides the ExtendSessionLi feBox 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 DisplayExtendSe ssionLifeBox() method after 115 seconds, and will call the HideExtendSessi onLifeBox() 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 New Member
@Frinny..
how can you give such long reply?? amazing..!!!
keep it up buddy.. :)
Oct 1 '09 #5
Frinavale
9,735 Recognized Expert Moderator Expert
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...be cause 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 "CloseExtendSes sionLifeBox()" method is already invoked before warning message box being displayed!

The solution would be to modify your code - calling
CloseExtendSess ionLifeBox() infinitely. So adding this statement within the "CloseExtendSes sionLifeBox()" 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 Recognized Expert Moderator Expert
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 CloseExtendSess ionLifeBox() method.

-Frinny
Oct 19 '10 #8

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

Similar topics

1
7998
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 working on the site or logout. Thanks
1
2280
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 value? ??? Muench
4
4845
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 logging out? i.e: using the "X" in the right corner??? i'd like to reset to their default all my variables session?? is this possible??? 2: I've succesfully been able to ask the user to confirm whether he wants to extend his session before it...
4
3890
by: Yuriy | last post by:
Hi, everybody! I need every 20 min extent my Session timeout. Any suggetions appreciated! Thanks
0
1100
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 on the server as expected. But if I invoke this page from an active session, this page extends the session expiration by the timeout minutes. I am expecting this page not to have any effect as to when the current session timeouts. I don't...
2
1449
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
5208
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 things before they submit the page. Unfortunately, if they do take their time, there are times where the session times out and they can no longer submit their information, resulting in an error and pretty peeved users. What I'm asking for help on...
5
2539
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 seesion for all users or only the user who intiated the call from the script? If not how can I set intranet users to have a time out of 1hr and internet users 15mins any help would be greatly appreciated
1
237
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 number of intranet applications that require session timeout to be more than this as many users get interupted by lengthy phone calls while they are completing their webforms. We therefore need to extend the session timeout for some of the...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.