Don't Forget to session_start()!

Posted on Nov 1st, 2021 by

Are you trying to get to grips with how sessions work in PHP, but having trouble getting PHP to pick up the session you started in a previous request? If so, you may be the victim of unfortunate function naming.

You might (reasonably) assume from the name that you only need to call session_start() to create the session, but you actually need to use it to resume the session for the current request, too. From the PHP manual (emphasis mine):

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

So, make sure to call session_start() whenever you want to do anything with the session, not only when you want to start a new one. (You could also set the session.auto_start setting to 1, which tells PHP to automatically start the session for every request.)

Sign up for our newsletter

Get more content like this delivered straight to your inbox.

We'll never send you spam or share your email.