PHP Session Array: Keep Track of Your Users

php session arrayPHP is an acronym for PHP: Hypertext Preprocessor. If you’re familiar with how a web page is actually built, you know that HTML is used to display the text and the structure of the web page, while JavaScript is what makes it interactive. So how is PHP useful? PHP does what HTML (and JavaScript) alone can’t accomplish. PHP can do a wide variety of things. It works on the server side and not on the client side like JavaScript. It can interact with a MySQL database on the server and retrieve information to be displayed in your webpage. It can be used to create simple graphics and icons, like the captcha codes you have to match when you submit forms. PHP can also be used to perform calculations, like displaying the time and data to someone who is browsing your webpage. The PHP code is embedded inside regular HTML code (along with JavaScript and CSS, if needed) to perform operations when necessary. Website development isn’t the only thing that PHP can accomplish- it can also be used as a general programming language. If you’re interested in learning the language, including the basics, we offer an introductory PHP course you can take. The language is easy to pick up, especially if you have a background in programming.

In this tutorial, we’re going to give you more information about PHP sessions and PHP session arrays. Don’t worry if you’re a PHP newbie- we’ll walk you through the basics.

PHP Sessions

Before we take a look at how to create PHP session arrays, do you know what sessions are? Whenever you log in to your computer, you have to specify a username and perhaps a password. After that, you will have access all your applications and programs. Your computer will always know who you are. But what happens when you surf the internet? You don’t have a stable identity. To distinguish your computer from the other users accessing the website at the same time, the server assigns begins a session and assigns your computer a unique ID. Sessions help the server keep track of your computer and save any changes you have made to the website (to your account, for example). Sessions are implemented with the help of PHP. Learn more about PHP sessions with this course.

Creating a PHP Session

A PHP session is started with the session_start () function. This function has to be written above the html tag in your program:

<?php session_start();   ?>
<html>
</html>

Now that you have initiated the session, you can assign a unique identification number (UID) to every computer that accesses your website. You can also create a PHP session array to store user information (user name, password, etc.).

Example of a PHP Session

Before we create a PHP session array, we’ll show you how to create a simple PHP session. This will help you understand how a typical PHP session works:

<?php
 session_start();
 ?>
 <html>
 <body>
 <?php
 echo "Congratulations, you have started a session’];
 ?>
 </body>
 </html>

Output:

Congratulations, you have created a session

The session_start functiona always comes at the beginning of the program. Remember, PHP lets you retrieve data from your server too. In this example, we’ve used PHP to print a simple statement. If you’d like to learn how to write your own PHP programs from scratch, this course is a great place to get started.

Session Variable

You can use the $_SESSION variable to store a variable. You can then retrieve this variable later. Take a look at this example:

<?php
 session_start();<
//store a global variable
 $_SESSION['time’]=1;
 ?>
 <html>
 <body>
 <?php
 //Now you can retrieve the previously declared variable
echo "Your session has now begun. It will last for the following number of hours:". $_SESSION['time'];
 ?>
 </body>
 </html>

Output:

Your session has now begun. It will last for the following number of hours: 1

Notice that we initialized the $_SESSION variable time at the beginning of the program, inside the <?php ?> declaration statement. Later, you can retrieve it from within your program. The $_SESSION variable is also referred to as a session array, because you can declare a session “variable” to be an array. Sounds confusing? It’s not, really.

PHP Session Array

Just like session variables, you can use an array to keep track of your users. The array may include variables like the UID of the user, the user name and the password. A session array, like a regular array, can theoretically hold any type of information you could think of.

We’ll show you some of the ways in which you can create an array inside your session. Take a look at this example:

<?php
$userinfo = array();
$userinfo['username'] = 'currentusername';
$userinfo['isloggedin'] = false;
$userinfo['UID'] = 1;
$_SESSION['userinfo'] = $userinfo;
?>

Do you see what we’ve done here? We’ve declared an array called userinfo and stored information inside it, like the login name, if the user is currently logged in or not and finally the UID. You can declare a normal array and then declare it as a $_SESSION variable later, which is what we’ve done in our example.

Here’s another way you could have declared the same array:

$_SESSION['userinfo']['username']  = 'currentusername';
$_SESSION['userinfo']['isloggedin']  = false;
$_SESSION['userinfo']['UID']   = 1;
?>

This is just another way to declare a PHP session array. Another way you could have created a Session array is to declare the variables earlier and then create an array to store them. You can use any way that appeals to you.

If you want to print any of the information given here, you can use the following line of code at the end of the array declaration:

var_dump($_SESSION['userinfo']);

The var_dump command gives detailed information about the array you’ve created.

Closing a Session

You can close a session with the session_destroy () function. This function has to be called in the PHP declaration statement as well:

<?php
 session_destroy();
 ?>

This will close your session and any session array you have created will be deleted.

Do go ahead and try writing your own PHP code. If you need help, you can take this course to learn how to create your own secure login script in PHP.