intelliproject logo

Location: Scripting language - JavaScript    License: The Intelliproject Open License (IPOL)

Keep session alive with Javascript

Posted by Silviu Caragea

The goal of this article is to present a very simple method that can be used to keep a session alive.

Skill: Beginner

Posted: 15/11/2008

Views: 14328

Rating: 5.00 /5

Popularity: 0.00

Sign Up to vote for this article


The goal of this article is to present a very simple method that can be used to keep a session alive.
Sometimes it takes to long to fill out a form or the user can leave the desk and comes back after a while. When the user will try to submit the form normally the server answers with a login screen or an error messages because the session has expired.

How can we avoid the session expiring?

To avoid the session expired you can choose different methods. I had saw around the internet peoples that had tried to change the session expire timeout but I will never recommend this method.

First of all for each session the server allocates resources that are released when the session is killed. So for this reason we should avoid to allocate resources that are not released on time.

In my opinion the best and elegant way to keep a session alive is to make permanents requests to the server directly from the client.

I found this method on the internet and I used it with success in all my websites.

The idea is to reload a very small transparent image (1x1) time to time to avoid the session expiring.

You should put the image somewhere into your webpage and this will be reloaded every time. In this article the image id is “image_alive”. After that set the image width and height to 0 to make it invisible to the client. 
This script reloads an image called keepalive.gif at every 100 seconds so the session will be kept alive as long as the browser stays opened. To prevent the image cache we add to the end of the image name a random number so this is the reason of the question mark from the end of the image name.

You can take a look in this page source code and you will see that use this method to prevent the session expiring.


This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)

About the author

Silviu Caragea

Silviu Caragea is the Founder, Administrator and Chief Editor who wrote and runs The IntelliProject.

He's been programming since 2000 and now he's student at The Faculty of Economic Cybernetics, Statistics and Informatics from Bucharest. In the same time he's working as software developer at Cratima Software, a Romanian software and web design company that activates both on the local and foreign market, providing its customers with software development services, internet and intranet solutions, web design, graphic design and IT consultancy.

His programming experience includes:
- C,C++, Visual C++(Win32 API, MFC, ADO, STL, DAO, ODBC, ATL, COM, DirectShow, DirectDraw, WTL)
- Open Source libraries :CURL & Boost
- Java (SE,ME)
- JavaScript, Ajax, Google Web Toolkit (GWT)
- Php, MySQL
-Oracle, PL SQL
- C# .NET
-Objective C, IPhone SDK, Cocoa

Location: Romania
Ocupation: Software Engineer
Home page:

Posted by Deepankur Singh at 23/07/2012 18:50
Will this Java script work in case of Web Farms as well, where in we have more than 1 web server where the application is hosted?
Posted by Silviu Caragea at 27/07/2012 19:35
You can give a try. Normally I think it should work.

Sign up to post message on the article message board!