Forum Moderators: open

Message Too Old, No Replies

Keeping a session alive

         

bleak26

1:55 pm on Jun 20, 2006 (gmt 0)

10+ Year Member



Is it possible to use Ajax to keep the session alive, by querying PHP somehow at regular intervals?

eelixduppy

6:03 pm on Jun 20, 2006 (gmt 0)



Why not just put session_start(); at the top of each page if you have PHP?

alanderson

3:48 am on Jun 21, 2006 (gmt 0)

10+ Year Member



Yes it is possible but it takes a bit of trickery.

The method has been called "Comet" by Alex Russell [alex.dojotoolkit.org]

It has also been called the slow load technique.

It is not terribly new but it is getting new life. If you remember the days before the browser supported animated GIFs there was a trick back then of sending a gif from a perl script and when it finished you ran the sleep command and sent the same gif again. This kept a connection pinned up between the browser and the server and drove system administrators batty.

In order to continue to stay on their good side we never used this on a large scale application and we did not tell people how to do it. (if connections do not close then the server will run out of available connections with less traffic.)

However Apache 2.2 is supposed to support these pinned up connections and if you don't mind the dirty looks from the sysadmin this is how it is done.

Write a cgi that runs slow. To do this you will send some javascript variables and then sleep and send more javascript variables and sleep. When you want to trigger something in the browser send a call to a function in the parent window. then go back to the loop.

On the browser, have a zero sized frame open the slow cgi and just let it run.

To see some sample code, Bryce Nesbitt wrote a simple description. [obviously.com]

Remember, until webservers optimize this kind of behavior it is not advisable with high traffic sites (we don't all have google's resources). Check with your sysadmin or maybe just make him wonder why the webserver is suddenly requiring many more connections that are not dropping.

Use it in good health.

Al Anderson