Sessions being dropped

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dhampson
Forum Newbie
Posts: 19
Joined: Mon Mar 24, 2008 8:01 pm

Sessions being dropped

Post by dhampson »

So, I've created a small piece of software for a business. It's only used on our internal network, so security is no /too/ big an issue. Never-the-less, I have people log in with a password, because each person has several students, and no one has access to each others student.

I simple use this to create my sessions:

Code: Select all

   
    session_name("Company-DB");
    session_start();
 
Passwords are stored in the $_SESSION array.

This has worked fine, but every once in a while, it will fail to connect to the mysql data base. This means any work is lost, and you need to start the whole thing over again. This seems to happen after a bit of inactivity. However, if you open another browser window and visit other sites, it can drop your session after just a few minutes. This only occurs once a week or so, but can be very annoying when it does. It happens with Safari, IE, and Firefox.

Would creating a time limit prevent this from happening? Would modifying the .ini file help? If so, how?

Any help or comments would be appreciated.

--Dave
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Sessions being dropped

Post by flying_circus »

How long of a period of inactivity? Sessions have a lifetime.
dhampson
Forum Newbie
Posts: 19
Joined: Mon Mar 24, 2008 8:01 pm

Re: Sessions being dropped

Post by dhampson »

Sessions are being dropped after a period of inactivity ranging from 15 minutes to 3 hours. It's a probability thing. If I left a browser window open overnight, I can guarantee that the session will be dropped. If I visit others web sites, it increases the probability of sessions being dropped sooner.

I did not declare a time limit when creating a session, because it's my understanding that the session is supposed to last until the browser window is closed this way. I don't want to create a time limit, unless I also create a javascript timer to alert users that time is running out. It is a very chaotic environment, and sometimes we have to leave our computers for a few hours. An alert could just be ignored.

Anyway, and thoughts as to why session are being dropped -before- the window is closed?
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Sessions being dropped

Post by flying_circus »

Session lifetime is not really based on the length that the window is open. Since it is a server side thing, the lifetime starts dying once the last time you've accessed a page on your site. If you're running a stock setup, the session gets stored as a text file, and then marked as garbage after so long of inactivity. You can change those settings in your php.ini under the [sessions] heading.

If you want them to last longer, you should be able to modify the php.ini to do what you need, otherwise, you can implement your own session handler using the session_set_save_handler() function.

http://us.php.net/manual/en/function.se ... andler.php
dhampson
Forum Newbie
Posts: 19
Joined: Mon Mar 24, 2008 8:01 pm

Re: Sessions being dropped

Post by dhampson »

Thanks Mr. Circus. 8) I'll try modifying the php.ini file.

I am also upgrading the server as well. It's an old AMD, and the Linux kernel in it has a small bug that fails to keep track of time on the AMD processors. Every week or so I manually reset the internal clock because it's about 90 minutes fast. I don't see how that could effect things, but you never know.........

--Dave
dhampson
Forum Newbie
Posts: 19
Joined: Mon Mar 24, 2008 8:01 pm

Re: Sessions being dropped

Post by dhampson »

Session lifetime is not really based on the length that the window is open. Since it is a server side thing, the lifetime starts dying once the last time you've accessed a page on your site. If you're running a stock setup, the session gets stored as a text file, and then marked as garbage after so long of inactivity. You can change those settings in your php.ini under the [sessions] heading.
OK. The variable I think needs to be changed is session.gc_maxlifetime. Is this correct?

--Dave

(I upgraded to Ubuntu 7, and the new kernel has the AMD-clock bug fixed. I thought that might alleviate the problem, but alas, it now times out quicker.)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Sessions being dropped

Post by s.dot »

dhampson wrote:OK. The variable I think needs to be changed is session.gc_maxlifetime. Is this correct?
Yup. It's value is in seconds. So to make it 3 hours.. 60*60*3 = 10800.

I think the default is 1440 which is 24 minutes.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply