Ever wonder how a website can remember your information on each page as you browse the site. The website is probably using sessions. With sessions, you can store a user’s information (username, address, shopping cart data, etc.) and then access this information at a later time.
In this PHP tutorial, I will teach you how to start a PHP session, store information to that session, retrieve data from the session, and then finally destorying the PHP session.
Before we begin, there are a few things you may be interested in learning about sessions in PHP.
- Sessions work by giving the user a unique identifier. No two unique identifiers are alike and are similar to this: c8ea114be0705a49083c44c2cf719a4e
- PHP Sessions are temporary and usually will be destoryed when a session is over or when a user closes a browser.
PHP sessions – Starting A Session
When you create a session, you must first place it before any html code. Below in the PHP code that will start a session and therefore will create a unique identifier for that user.
<?php
//start a PHP session
session_start();
?>
Pretty simple to start a PHP session!
PHP sessions – Storing Information Into A Session
So you have started a session on a page and now what to store some data in the session. To store data into a session we use a predefined variable called $_SESSION. In the PHP code below we will add a username to the session.
<?php
//Start a sesssion in PHP
session_start();
//Store username data into the session
$_SESSION['username'] = “Joe”;
?>
PHP sessions – Retrieving Information In The Session
We have started a session and have stored a username in the session, now we will retrieve the username from the session. The PHP code below will get the username from the session.
<?php
//Start a sesssion in PHP
session_start();
//Print out the value that is stored in the session for your the username.
echo $_SESSION["username"];
?>
The outcome will be: Joe
PHP sessions – Destorying a session
Like I said early in this tutorial, a session is destoryed when a user leaves your site but there is another way you can destory a session. To destory a session, we will use the unset() function that is available for you. The PHP code below will destroy a PHP session.
<?php
//Start a sesssion in PHP
session_start();
//Remove the username data in the session
unset($_SESSION["username"]);
?>
This will only remove the username data. If you had other data in the session, say shopping cart data, you could still get at that. To destory the entire sesssion you would use the session_destory() function. Below is the PHP code to do this.
<?php
//Start a sesssion in PHP
session_start();
//Destory a session in PHP
session_destroy();
?>
PHP sessions – Using The isset() Fuction
The final topic to bring up with sessions is the isset() function. The isset() function allows you to check in a variable within the session is already set.
Lets say you want to assign a username to the session. First you should check if that variable is already set. The PHP code below will use the isset() function to check if the username variable is set.
<?php
//Start a sesssion in PHP
session_start();
if(isset($_SESSION['username']))
//if the username is already set you will print it out.
echo $_SESSION["username"];
else
$_SESSION['username'] = ”;
?>
The first time the page is loaded you will set the username variable otherwise it will print out the username varible. Pretty Simple!!
No related posts.