Click here to Skip to main content
Click here to Skip to main content

Make session last forever

, 23 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
This article will show how to make the session last longer than 20 minutes of inactivity.

Introduction

You do not always have the luxury to bump up the session expiration period from 20 minutes on a server (most likely you are on a shared hosting). Plus it's a bad idea to unnecessarily consume the resources on a server. I have a simple solution that will take care of this problem.

Solution

The solution is simple. Let's create an ASPX page that will return a 1x1 transparent GIF image, and using JavaScript, refresh it every 15 minutes. Thus, the browser will renew the session every 15 minutes if the user is simply reading long text on a page or has walked away to get a cup of coffee. But if the browser is closed (or the user navigates away from our site), the session will expire after 20 minutes of inactivity. And we no longer consume the resources on the server.

Implementation

The implementation is rather simple, especially in the .NET environment.

  1. Create a renewSes.aspx page
  2. In the code-behind, add the following lines:
  3. public class renewSes : System.Web.UI.Page
    {
        static byte [] gif = {0x47,0x49,0x46,0x38,0x39,0x61,0x01,0x00,0x01,
                              0x00,0x91,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                              0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x21,0xf9,
                              0x04,0x09,0x00,0x00,0x00,0x00,0x2c,0x00,0x00,
                              0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x08,0x04,
                              0x00,0x01,0x04,0x04,0x00,0x3b,0x00};
    
        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }
        
        private void InitializeComponent()
        {    
            Response.AddHeader("ContentType", "image/gif");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.BinaryWrite(gif);
            Response.End();
        }
    }

    This code will simply output a small 1x1 transparent GIF, which is in the array "gif", so we do not need to hit the file system for that. Plus, Cacheability is set to NoCache as we do not want the browser to hit the cache, but we want it to renew our session.

  4. Create a user control that has the following HTML:
  5. <script language="Javascript">
    window.setInterval("renewSession();", 600000);
    function renewSession()
    {document.images("renewSession").src = "/renewSes.aspx?par=" + Math.random();}
    </script>
    <img src="/renewSes.aspx" name=renewSession id=renewSession>

    The code simply hits /renewSes.aspx every 10 minutes, thus keeping your session alive. Plus, Math.random was added to avoid caching in the browser.

  6. Dump it on every page somewhere. Or if you are using templates, then simply add it to your master page.
  7. Done!!! You might want to move the JavaScript code to RegisterClientScriptBlock, but it's not necessary.

Conclusion

Obviously, this method will only work if JavaScript is on, but it's a majority, plus do not forget that it's only for rare cases when the user leaves your page open and walks away expecting you will keep him logged in forever. The server resources are minimal since sessions will continue to expire every 20 minutes when the user navigates away, and the only excessive traffic is the 48 bytes every ten minutes to keep the session alive.

Sample

If you want to see how it works, feel free to visit my E-Commerce Body jewelry site. Add something to the shopping cart and wait for 20 minutes or for a couple days. When you will be back, the cart is still going to be there.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gevorg
Web Developer
United States United States
I am a Senior Software developer who does a consulting work for several companies. Mostly it's an E-Commerce project.
 
I do have my own venture. Small online store my wife runs. Body Jewelry. Also visit Piercing info Cool thing is that nor I nor my wife has any piercings. My new website is Belly ring

Comments and Discussions

 
GeneralMy vote of 1 PinmemberJason Ti20-Dec-10 23:40 
GeneralRainbow jewels Pinmemberimaginaryplaymate20-Dec-08 23:38 
Questionthe same with asp? Pinmemberran920-Nov-07 1:26 
GeneralDreamers! Pinmemberbeep1-Nov-07 6:40 
QuestionWhy using a random number? PinmemberAdam Tibi1-Dec-06 0:13 
Generalnot fully argee PinmemberEffun Din30-Oct-06 22:36 
GeneralRe: not fully argee PinmemberAdam Tibi31-Oct-06 4:49 
GeneralRe: not fully argee PinmemberEffun Din31-Oct-06 5:17 
GeneralAnother improvement PinmemberDejaVudew10-Oct-06 4:07 
GeneralRe: Another improvement PinmemberGevorg10-Oct-06 9:41 
AnswerRe: Another improvement PinmemberDejaVudew10-Oct-06 13:01 
GeneralRe: Another improvement PinmemberGevorg12-Oct-06 9:14 
GeneralAnother aproche PinmemberZorbek9-Oct-06 14:22 
GeneralProfile? [asp.net 2.0] PinmemberBigBenDk21-Aug-06 12:48 
GeneralRe: Profile? [asp.net 2.0] PinmemberGevorg22-Aug-06 3:34 
GeneralRe: Profile? [asp.net 2.0] PinmemberMivano7-Oct-06 5:06 
GeneralSlight modification PinmemberSymo S21-Aug-06 7:11 
GeneralRe: Slight modification PinmemberTrevahaha28-Nov-07 16:25 
GeneralRe: Slight modification Pinmemberjonnoshaw3-Apr-09 17:06 
GeneralAmazing Work !! Pinmemberbobtxt8-Aug-06 23:23 
GeneralIts a good idea but here's a slight improvement PinmemberRobert Ensor30-Jul-06 18:21 
GeneralRe: Its a good idea but here's a slight improvement PinmemberGevorg31-Jul-06 5:38 
AnswerRe: Its a good idea but here's a slight improvement PinmemberAdam Tibi1-Aug-06 3:19 
GeneralRe: Its a good idea but here's a slight improvement PinmemberGevorg1-Aug-06 12:09 
GeneralRe: Its a good idea but here's a slight improvement [modified] PinmemberRobert Ensor1-Aug-06 12:53 
AnswerRe: Its a good idea but here's a slight improvement PinmemberAdam Tibi1-Aug-06 22:38 
QuestionWhat about db? Pinmemberian.coetzer28-Jul-06 6:19 
AnswerRe: What about db? PinmemberGevorg28-Jul-06 8:06 
GeneralRe: What about db? Pinmemberptmcomp1-Sep-06 10:46 
AnswerRe: What about db? PinmemberJM_G23-Oct-06 22:49 
GeneralMy Girlfriends PinmemberVarindir Rajesh Mahdihar27-Jul-06 11:36 
GeneralRe: My Girlfriends PinmemberGevorg28-Jul-06 5:55 
QuestionErr? PinmemberMark Cassidy18-Jul-06 23:11 
AnswerRe: Err? PinmemberGevorg20-Jul-06 12:16 
GeneralRe: Err? PinmemberMark Cassidy20-Jul-06 22:20 
GeneralSuggestion PinmemberRichard Deeming14-Jul-06 8:23 
GeneralRe: Suggestion PinmemberGevorg14-Jul-06 8:39 
GeneralRe: Suggestion Pinmembersayedwasim14-Aug-06 21:26 
GeneralRe: Suggestion PinmemberRichard Deeming15-Aug-06 1:24 
GeneralSo Simple PinmemberBoro_Bob14-Jul-06 0:09 
GeneralScalability PinmemberMark Focas13-Jul-06 20:48 
GeneralRe: Scalability PinmemberGevorg14-Jul-06 8:28 
GeneralRe: Scalability PinmemberBrian.Brown27-Jul-06 11:40 
AnswerRe: Scalability PinmemberC# Genius29-Jul-06 10:27 
GeneralRe: Scalability PinmemberGevorg1-Aug-06 11:57 
GeneralRe: Scalability PinmemberC# Genius1-Aug-06 17:52 
GeneralRe: Scalability PinmemberGevorg3-Aug-06 7:44 
GeneralRe: Scalability PinmemberBrian.Brown21-Aug-06 10:06 
GeneralRe: Scalability PinmemberBrian.Brown21-Aug-06 9:56 
GeneralMeta refresh Pinmembermischa_k30-Apr-06 23:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150128.1 | Last Updated 23 Oct 2006
Article Copyright 2006 by Gevorg
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid