We help IT Professionals succeed at work.
Get Started

Managing PHP sessions and timeouts

Brad Bansner
Brad Bansner asked
Last Modified: 2015-03-25
I am a beginning PHP programmer with a lot of Classic ASP experience. I am creating a new web app where users login and the login status is controlled with a PHP session. I have this bit of code inserted at the beginning of all my pages, via an include file:


if (!isset($_SESSION["user_id"])){
} else {
	if ($_SESSION["user_id"]==""){
	} else {

Open in new window

For pages where I don't want non-logged in users to have access, I insert this at the top:

if ($user_login==0){
	header("Location: default.php", true, 303);

Open in new window

That simply bounces them back to the home (login) page if $user_login==0, rather than loading the rest of the page. If they are logged in ($user_login==1), then the page will load.

When the user clicks a "log out" button, I simply call this:


Open in new window

All of that works fine. The odd thing is that my sessions don't seem to timeout. It is my understanding that a PHP session will timeout after 20 minutes of inactivity (similar to an ASP session). However, even after letting my browser window sit idle for over an hour, if I hit "reload", the browser does not redirect backt to the home (login) page.

Is there something I am missing? Also, is the way I am coding sessions in the above examples pretty solid, as far as PHP goes? Would appreciate any advice or critique on what I'm doing.

Thank you.
Watch Question
Fixer of Problems
Most Valuable Expert 2014
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE