7

I am wondering whether we can declare session.gc_maxlifetime setting in .htaccess for the one particular project instead of whole web server?

If so, how can we do that? Like the following code?

php_value session.gc_maxlifetime 2000

I've tried it and it didn't work and also I created a php.ini file in the same directory of my project and it didn't work either.

Thanks.

Tarik
  • 79,711
  • 83
  • 236
  • 349

2 Answers2

20

Yes, session.gc_maxlifetime is a PHP_INI_ALL setting so it can be overidden in .htaccess:

php_value session.gc_maxlifetime 2000

Also make sure that <Directory> entry in your Apache configuration supports override:

AllowOverride Options

It also may be possible that you misunderstood the purpose of this option. This option will not set the maximum life time of a session, it will set the amount of time after which the garbage collector will clean the session if it's not valid. This can be determined by a lot of factors, including access times, modification times, other INI options such as session.gc_probability and session.gc_divisor.

If you want to limit a session life time, use a proper mechanism for that, as described by @Gumbo in How do I expire a PHP session after 30 minutes.

Community
  • 1
  • 1
netcoder
  • 66,435
  • 19
  • 125
  • 142
  • How to make it overridable? Thanks. – Tarik Jun 06 '11 at 14:50
  • @Braveyard: It's already in my answer. It's the `AllowOverride` directive. Make sure you read the entire answer, because I still don't think you know what *gc.maxlifetime* really does. – netcoder Jun 06 '11 at 14:53
  • Thanks for the answer. I think I know more about that setting. I like the link by the way. – Tarik Jun 06 '11 at 18:31
  • That should be an underscore in gc_maxlifetime (took me an while to find this after I copied it) – Liam Jun 25 '12 at 16:53
  • this one helped me when im setup domain at hostinger :D – Yohanim May 21 '21 at 08:45
3

As far I can see, the gc_maxlifetime value can be set anywhere:

http://php.net/manual/en/session.configuration.php

http://www.php.net/manual/en/configuration.changes.modes.php

So you could also set it via ini_set(...) in a php file included in all your pages.

Anyway, I think your code in the .htaccess should work as well. Maybe you missed something else, for example

php_value session.save_path "/PATH/TO/SESSIONS"

where PATH/TO/SESSIONS is the path of a folder where you have 777 access permissions.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Fabrizio D'Ammassa
  • 4,729
  • 25
  • 32