How to display logged-in user information in PHP?


In this article, we will learn how to display logged-in user information using PHP and its various inbuild methods.

When building a web application that requires authentication, it is often necessary to display the logged-in user's information on various pages. This could be useful in applications such as e-commerce websites, banking websites, and more. We can implement this with the help of PHP and its functions.

Let’s understand this with the help of some examples.

Example 1

In this example, we will create a login/logout system wherein the user will get authenticated once he is logged in, and will get redirected to the dashboard page with his information visible. The user can then logout from the dashboard to reset the session.

Filename: login.php

<?php
   session_start();

   if (isset($_POST['username']) && isset($_POST['password'])) {
      $username = $_POST['username'];
      $password = $_POST['password'];

      // Check if username and password are correct (e.g. compare with database)
      // For simplicity, this example only checks if username is 'admin' and password is 'password'
      if ($username === 'admin' && $password === 'password') {
         $_SESSION['username'] = $username;
         header('Location: dashboard.php');
         exit();
      } else {
         $error_message = 'Invalid username or password';
      }
   }
?>

<html lang="en">
<head>
   <title>How to display logged in user information in PHP?</title>
</head>
<body>
      <?php if (isset($error_message)): ?>
         <p><?php echo $error_message; ?></p>
      <?php endif; ?>

   <form method="post">
      <label>
         Username:
         <input type="text" name="username" required>
      </label>

      <br>

      <label>
         Password:
         <input type="password" name="password" required>
      </label>

      <br>

      <button type="submit">Log In</button>
   </form>
</body>
</html>

Filename: logout.php

<?php
   session_start();

  // Unset all of the session variables
   $_SESSION = array();

   // Destroy the session
   session_destroy();

   // Redirect to the login page
   header("Location: login.php");
   exit;
?>

Filename: dashboard.php

<?php
   // Start the session
   session_start();

   // Check if user is logged in
   if (!isset($_SESSION['username'])) {
      header("Location: login.php");
      exit;
   }

   // Retrieve user information from session
   $username = $_SESSION['username'];
?>

<html lang="en">
<head>
   <title>How to display logged in user information in PHP?</title>
</head>
<body>
   <p>Your username is: <?php echo $username; ?></p>
   <p><a href="logout.php">Logout</a></p>
</body>
</html>

Example 2

In this example, we will display the logged-in user's information on the profile page. The user needs to be authenticated to access the profile page.

Filename: login.php

<?php
   session_start();
   
   if (isset($_POST['username']) && isset($_POST['password'])) {
      $username = $_POST['username'];
      $password = $_POST['password'];

      // Check if username and password are correct (e.g. compare with database)
      // For simplicity, this example only checks if username is 'admin' and password is 'password'
      if ($username === 'admin' && $password === 'password') {
      $_SESSION['username'] = $username;
      header('Location: dashboard.php');
      exit();
      } else {
         $error_message = 'Invalid username or password';
      }
   }
?>

<html lang="en">
<head>
   <title>How to display logged in user information in PHP?</title>
</head>
<body>
   <?php if (isset($error_message)): ?>
      <p><?php echo $error_message; ?></p>
   <?php endif; ?>

   <form method="post">
      <label>
         Username:
         <input type="text" name="username" required>
      </label>

      <br>

      <label>
         Password:
         <input type="password" name="password" required>
      </label>

      <br>

      <button type="submit">Log In</button>
   </form>
</body>
</html>

Filename: logout.php

<?php
   session_start();

   // Unset all of the session variables
   $_SESSION = array();

   // Destroy the session
   session_destroy();

   // Redirect to the login page
   header("Location: login.php");
   exit;
?>

Filename: profile.php

<?php
   // Start the session
   session_start();

   // Check if user is logged in
   if (!isset($_SESSION['username'])) {
      header("Location: login.php");
      exit;
   }

   // Retrieve user information from session
   $username = $_SESSION['username'];

   // Simulate retrieving user information from database
   $user_info = array(
      'name' => 'John Doe',
      'email' => 'john.doe@example.com',
      'phone' => '1234567890',
   );
?>
<html lang="en">
<head>
   <title>Profile Page</title>
</head>
<body>
   <h1>Welcome, <?php echo $username; ?></h1>
   <h2>Profile Information</h2>
   <p>Name: <?php echo $user_info['name']; ?></p>
   <p>Email: <?php echo $user_info['email']; ?></p>
   <p>Phone: <?php echo $user_info['phone']; ?></p>
   <p><a href="logout.php">Logout</a></p>
</body>
</html>

Conclusion

In this article, we learned how to display the user information of a logged-in user in PHP. By following the simple steps outlined above, we easily retrieved and displayed user information on any page of our web application. This allowed us to provide a personalized experience for each user and make our application more user-friendly.

Updated on: 03-Aug-2023

862 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements