Sessions

Sessions enable personalized and stateful experiences

What is a session? It’s a way to store data on the server without using the database. When a user goes from page to page, each request generates a new set of variables that will be used to help generate the pages that the user needs to see. The variables are temporary, meaning that once the user visits another page, those variables in your code are gone. In order to have data persistence so far, we’ve used the database.

If you need permanent data persistence, you should absolutely continue using the database. But what if you need to store some data that’s only used for the life of the user’s session (the time that they’re actively visiting your web-application)? Can you still use a database? Of course. But it’s slower and somewhat unnecessary.

PHP has this thing called a session. The session is a variable, $_SESSION, that allows for data to be stored. The $_SESSION variable is a superglobal array variable.

Sessions are enabled by default and do not require any kind of external library or configuration.

How does the server not confuse the user’s data with other user’s data? When the session is initialized, it’s set against a unique user id that’s generated by PHP for each user.

In order for sessions to persist data across pages, each page that’s using the $_SESSION will need to be started with the session_start() function. This will be at the top of your file.

Example

Enough explanation, let’s see it in action. We’ll create two files: cart.php and add_item.php. The cart will display the total and the add_item page will add 100 each time to the total value.

Let’s take a look at the cart first.

<?php
session_start();

if ( ! isset($_SESSION['total']) ) {
    $_SESSION['total'] = 0;
}

echo "The total price for items in your cart is: " . $_SESSION['total'];

We start off by calling the session_start() function. This will allow our page to use the $_SESSION variable. Next, we’ll check to see if the $_SESSION['total'] array key exists. Remember that the $_SESSION superglobal is an array. If it doesn’t exist, we’ll initialize it to 0. Next, we’ll display the total to the user.

In the first iteration when we visit the cart, we’ll get: The total price for items in your cart is: 0.

Next up is the add_item page.

<?php
session_start();

if ( ! isset($_SESSION['total']) ) {
    $_SESSION['total'] = 100;
} else {
    $_SESSION['total'] += 100;
}

?>
Item has been added to the cart
<a href="cart.php">Visit the cart</a>

The session is started again. We need to check if the total is set since the user might visit this page before they visit the cart. If the session hasn’t been set, the $_SESSION['total'] will be initialized to 100. If the total is set, then we’ll just increment by 100.

After you visit the page, click on the Visit the cart link and you’ll see that your total is now 100. Go back to the add_item page again and visit the cart once more to see your total value increment to 200. This is only possible because you’re using the $_SESSION superglobal.

To clear out the total we could just set $_SESSION['total’] = 0. What if we wanted to remove the total completely from the $_SESSION variable? We use the unset() function.

<?php
session_start();

if ( isset($_SESSION['total']) ) {
    unset($_SESSION['total']);
}

var_dump($_SESSION);

?>
The $_SESSION['total'] has been removed

To completely delete the session for a particular user, use the session_destroy() function.

Summary

That’s it when it comes to sessions. This is temporary data persistence on the server side. Next we’ll look at cookies, which is data persistence on the client side.

Normalization

NORMALIZATION IN MYSQL STREAMLINES DATABASE ORGANIZATION

PHP – P93: MYSQL NORMALIZATION

The last topic is normalization. What does it mean to normalize a database? In a nutshell, removing duplicate content and streamlining your database.

Sessions

Sessions enable personalized and stateful experiences

PHP – P94: sessions

What is a session? It’s a way to store data on the server without using the database. When a user goes from page to page, each request generates a new set of variables that will be used to help generate the pages that the user needs to see.

Cookies

COOKIES REMEMBER USER PREFERENCES AND LOGIN STATUS

PHP – P95: COOKIES

A cookie is small text-file with user identifiable information that’s set on the user’s computer by the server. The cookie is sent to the server each time that a new request comes from the user.

Leave a Reply