PHP Session States

PHP

Creating a Session

Sessions exists in PHP to track an object - whether its and ID, text, user input and whatever more:

When working with "session_start()", its a good practice to include them at the very top of files, so they understand immediately, that if you are logged in, it kind takes the "passed" session start function from the page, where the log in started and reads that the user is currently logged in.

Sessions with Log Ins

Commonly, sessions are created when an user logs into account, so the website can track that the user is currently logged in:

Here, we are setting a new variable "$_SESSION['username']" for the user selected from database. You can put whatever into the "$_SESSION[]" block bracks - the "$_SESSION[]" variable just exists, but you can customize it and make new ones - a PHP feature.

Deleting a Session

To destroy a session, you can do this:

The "session_unset()" is needed, because to delete all variables tied to the item - else it would pile up as cache and weird session state glitches would occur.

Cart Session System

You can set items in a state, where they are yours, until you put them back - just like holding an item and putting item back later: