Extending php session timeout

I want to extend the timeout of a php session. Currently, on the web server, php.ini has session.gc_maxlifetime set to 1440 (20 minutes)

I found this code on the web:
// server should keep session data for AT LEAST 1 hour
ini_set('session.gc_maxlifetime', 3600);

// each client should remember their session id for EXACTLY 1 hour

session_start(); // ready to go!

If I want to extend the session duration to one hour (or whatever I want), can I use this code just once (where an authorized user logs in) or do I have to put it on EVERY php page that accesses session variables?

Richard KortsBusiness Owner / Chief DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Rather than extend the PHP session (which has a very specific purpose and works in a simple and predictable way) consider using a "remember me" cookie.  It's a client-facing design pattern that will make more sense to your clients because they have seen it in many other places!

This article explains the PHP session.

This article shows how to use a "remember me" cookie. You can set the expiration to any timeframe you want.  It will survive the browser being closed (for example, if a client goes away and comes back).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:

I would rather just extend the session. Does the code I presented need to be in EVERY php file which accesses a session variable or JUST at the login?

Dave BaldwinFixer of ProblemsCommented:
Note that if you are on a server with shared hosting, the shortest timeout will prevail and your 'extended time out' will never work.  In addition, the 'session.gc_maxlifetime' is just the shortest time for session and garbage cleanup.  It is Not a precise time.  The probability variable is used to determine when it will actually happen.

I think that what you want is basically a bad idea and I never do it.  Note that the timeout period is reset on every page access by the user so it is 20 minutes from the last time they accessed one of the pages in the session.  It is an Inactivity timeout, not any kind of precise session control.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Dave BaldwinFixer of ProblemsCommented:
Frankly, if there is data that needs to be remembered longer than that, you should be putting it in database and not $_SESSION variables.
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
After reading through Ray's "Remember Me" examples & code, I am going that route.

So in fairness, I asked EE to reopen the question so I could adjust the points.

Dave BaldwinFixer of ProblemsCommented:
Ok, thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.