How to extend PHP Session through .htaccess file

i have a site and i want to extend the session for a month

i wrote these lines in my .htaccess file :

php_value session.cookie_lifetime 2592000
php_value session.gc_maxlifetime 2592000

but still the session destroys after like an hour
i have tried a lot of things but didn’t come up with a result

Php settings in a .htaccess file only work when php is running as a server module.

If php is being executed via CGI (Common Gateway Interface), putting settings in a .htaccess file won’t work. In this case, you need to put the settings into either the main php.ini, a local php,ini, or in your php code so that those settings are set before every session_start statement.

Now, why do you want to do this, since the main purpose of session data is to only last one browser session?

1 Like

i did that but still not working

lsapi_phpini /home/youropin/public_html/php.ini

is this the right way to write the path of php.ini in .htaccess file ?

i’m doing this in only one page cause i want to interact with visitors without forcing them to register on the site

For something longer lasting than a session, maybe look at cookies.


i tried this

$cookie_name = "user";
$cookie_value = $variable;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");

but it’s not working , the cookie prints nothing
can’t figure out what’s wrong

That is for setting the cookie, how are you then reading the cookie to print it?

Do you see the cookie in your browser development tools (under the “Application” tab in the storage section… if you are using Chrome). Once you set it, you should then see it there while browsing the site where you set the cookie on.

I’m surprised nobody has mentioned Windows local storage which is browser specific and lasts forever until deleted. Also has a lot more storage than sessions or cookies I think, but could be wrong. I’m developing a local storage on a Shopping Cart:

1 Like

echo $_COOKIE[$cookie_name];