PHP session is very useful in web page design when using in a system like member area that require users to log in with their username and password.

What PHP Session Can Do?

Session variables can be used to save information, for example username and password, of a logged in user. The wonderful thing with session variables are that they are available to all web pages in the system.

Session variables that save in the hosting server is temporary and will be cleared when a user has log out or left the website.

In the old days before the introduction of session variables, we can only use the GET METHOD to pass the variables in the URL string, or using the POST METHOD with the <input type=”hidden” name=”username” value=”$username”> to pass the variable to other pages.

In summary, PHP session variables can be used to pass variables between pages in a single system, for example, member area.

In this PHP session basic concept tutorial, we will use a simple member area to illustrate how PHP session works.

PHP Session Simple Log In System

The most commonly use of PHP session is member area. Let’s use a simple member area to illustrate the basic concept of PHP session.

The simple member area include a home page, a member login page, other two normal pages and a log out or exit page. The file structure of the PHP session login system can be illustrated with the diagram below.

PHP Session Login System Home Page

The codes of the home page is very simple. Here’s how it works.

  1. Use the session_start() function to start saving the user information.
  2. The next thing to do is check if username session variable is saved in the server already. This is usually used to check whether the user click on the back button of the browser.
  3. If there is no username session variable save in the server, ask user to log in, otherwise simply print out the username information.


<?

// start the session, before the <html> tag, in order to store user information
session_start();

echo "<h2>Welcome to My Home Page</h2>";

// Check if username session variable is saved already
// If no username session variable saved, ask user to log in, otherwise
// print out the username information.
if (!isset($_SESSION['username'])) {

echo "<a href=\"page1.php\">Click here to log in.</a>" ;

} else {

echo "You log in already! " . "Your username is: " . $_SESSION['username'] . " | " . "<a href=\"exit.php\">Exit</a>" . "<hr />" . "<a href=\"page1.php\">Page 1</a>" . " | " . "<a href=\"page2.php\">Page 2</a>";

}

?>

PHP Session Login System Log In Page

To simplify the codes, the username is assigned in the codes. In a working web page, you have to ask the user to fill in the username and password.

  1. Check if username session variable is saved in the server already. If not, log in user.
  2. Use PHP $_SESSION to save the variable.


<?

session_start();

echo "<h2>This is Page 1</h2>";

// Check if username session variable is saved already
// If no username session variable saved, log in user, otherwise
// print out the username information.
if (!isset($_SESSION['username'])) {

$username = "alex";

$_SESSION['username'] = $username;

echo "Welcome : " . $_SESSION['username'] . " | " . "<a href=\"exit.php\">Exit</a>" . "<hr />" . "<a href=\"page2.php\">Page 2</a>";

} else {

echo "The username already saved in the server! You are log in already!" . "<br /><br />" ;

echo "Welcome : " . $_SESSION['username'] . " | " . "<a href=\"exit.php\">Exit</a>" . "<hr />" . "<a href=\"page2.php\">Page 2</a>";

}

?>

PHP Session Login System (Page 2)

Once log in the system, the user can navigate to other page. Of course this is important to check whether the user is check out already or not. This can be done by checking if the username session is saved in the server or not.



<?

session_start();

echo "<h2>This is Page 2</h2>";

// Check if username session variable is saved already
// If no username session variable saved, go to homepage, otherwise
// print out the username information.
if (!isset($_SESSION['username'])) {

echo "The username is not in server! " . "<a href=\"index.php\">Click here go to Home Page.</a>" ;

} else {

echo "Welcome! ". $_SESSION['username'] . " | " . "<a href=\"exit.php\">Exit</a>" . "<hr />" . "<a href=\"page3.php\">Page 3</a>" ;

}

?>

PHP Session Login System (Page 3)

A simple member area system will usually has a few pages. Same as other pages in the member area, this is important to check whether the user is check out already or not.



<?

session_start();

echo "<h2>This is Page 3</h2>";

// Check if username session variable is saved already
// If no username session variable saved, go to homepage, otherwise
// print out the username information.
if (!isset($_SESSION['username'])) {

echo "The username is not in server! " . "<a href=\"index.php\">Click here go to Home Page.</a>" ;

} else {

echo "Welcome! ". $_SESSION['username'] . " | " . "<a href=\"exit.php\">Exit</a>" . "<hr />" . "<a href=\"page2.php\">Page 2</a>" ;

}

?>

PHP Session Login System (Log Out Page)

For security purpose, a PHP member area system will usually include a log out page. This can prevent other users press the back button to see the information in case the member forget to turn off the computer.

Use PHP unset() or session_destroy() function to clear the session variables.



<?

session_start();

// clear username session variable with unset() function
// or clear the session completely with session_destroy function
unset($_SESSION['username']);

?>

<h2>You log Out the System!</h2>

<hr />
<a href="index.php">Click here go to Home Page.</a>

Using PHP Session in MySQL Query

Use the following syntax when using PHP session in MySQL query:

$sql = " SELECT username, password FROM member_table where username = '$_SESSION[username]' ";

DO NOT use the following syntax, it will generate an error:

$sql = " SELECT username, password FROM member_table where username = '$_SESSION['username']' ";

PHP Session Login System Demo

Click here to view PHP Session Basic Concept Tutorial Demo

This is just a basic concept of how PHP session works. Of course, you can further modify the codes to make a more practical member login system.

This is the end of simple PHP session simple login system tutorial.