Naming your sessions

David Carr

Tutorials PHP & MySQL

In PHP when you create a session and give it a name always make the name unique and not the same name as any variables you have.

For example if you create a session like this:

$_SESSION['username'] = $username;

You could override the session with the variable username a common example of this is if you had a list of user and had a query pulling out all the usernames from your database the loop would cause the last username in the loop to override the session, In this case the session is now the last user in the loop and is a major security vulnerability. 

To avoid this just make the session name unique from all variables used throughout the site like:

$_SESSION['loggedin'] = $username;

This makes sure it won't be over written and will save you hours of trying to find out what's going wrong.

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Fathom Analytics $10 discount on your first invoice using this link

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.