# Using PHP Sessions to Keep Users Logged In

In the previous tutorial, you created a login form that verified usernames and passwords. In this tutorial, you will use PHP sessions to remember who is logged in and keep them signed in while they navigate your website.

---

## What is a Session?

A session allows PHP to store information about a user while they move between pages.

Without sessions, a website would forget who the user is every time a new page loads.

---

## Start a Session

Open your existing:

```text
login.php
```

Add the following code at the very top of the file:

```php
<?php

session_start();

?>
```

The `session_start()` function must be called before any HTML is sent to the browser.

Your file should now begin with:

```php
<?php

session_start();

$conn = new mysqli(
    "localhost",
    "root",
    "",
    "project_db"
);
```

---

## Store User Information in the Session

Locate this section:

```php
if (
    password_verify(
        $password,
        $user["password"]
    )
) {

    echo "<p>Login successful.</p>";

}
```

Replace it with:

```php
if (
    password_verify(
        $password,
        $user["password"]
    )
) {

    $_SESSION["user_id"] = $user["user_id"];
    $_SESSION["username"] = $user["username"];

    echo "<p>Login successful.</p>";

}
```

When a user logs in successfully, their information is now stored in the session.

---

## Create a Members Page

Create a new file called:

```text
members.php
```

Add the following code:

```php
<?php

session_start();

?>

<!DOCTYPE html>
<html>
<head>
    <title>Members Area</title>
</head>
<body>

<h1>Members Area</h1>

<p>Welcome,
<?php echo $_SESSION["username"]; ?>
</p>

</body>
</html>
```

Save the file.

---

## Open the Members Page

Visit:

```text
http://localhost/members.php
```

If you have logged in successfully, you should see:

```text
Members Area

Welcome, admin
```

or whatever username was used to log in.

[![](https://mr.napper.au/uploads/images/gallery/2026-06/scaled-1680-/image-1780894712588.png)](https://mr.napper.au/uploads/images/gallery/2026-06/image-1780894712588.png)

## Display Additional Session Information

You can access any values stored in the session.

For example:

```php
<p>User ID:
<?php echo $_SESSION["user_id"]; ?>
</p>
```

Result:

```text
User ID: 1
```

---

## View the Session Data

Add the following code to the members page:

```php
<pre>
<?php
print_r($_SESSION);
?>
</pre>
```

Example output:

```text
Array
(
    [user_id] => 1
    [username] => admin
)
```

This can be useful when testing.


## Redirect Users After Login

Instead of displaying:

```php
echo "<p>Login successful.</p>";
```

replace the success code with:

```php
$_SESSION["user_id"] = $user["user_id"];
$_SESSION["username"] = $user["username"];

header("Location: members.php");
exit();
```

Now users will be automatically redirected to the members page after a successful login.

---

## Test the Complete Process

1. Open:

```text
http://localhost/login.php
```

2. Log in using an existing account.

3. You should be redirected to:

```text
http://localhost/members.php
```

4. The page should display your username.

> **Screenshot Placeholder**
>
> Insert screenshot showing the successful login redirect.

---

## Complete Login Success Code

```php
if (
    password_verify(
        $password,
        $user["password"]
    )
) {

    $_SESSION["user_id"] = $user["user_id"];
    $_SESSION["username"] = $user["username"];

    header("Location: members.php");
    exit();

}
```

---

## Complete Members Page

```php
<?php

session_start();

?>

<!DOCTYPE html>
<html>
<head>
    <title>Members Area</title>
</head>
<body>

<h1>Members Area</h1>

<p>Welcome,
<?php echo $_SESSION["username"]; ?>
</p>

<p>User ID:
<?php echo $_SESSION["user_id"]; ?>
</p>

</body>
</html>
```

You now have a working login system that remembers users between pages using PHP sessions.

Next tutorial: **Protecting Pages and Preventing Unauthorised Access**.