Home > Backend Development > PHP Tutorial > PHP code: Method to display logged in user information

PHP code: Method to display logged in user information

WBOY
Release: 2023-09-16 12:34:01
forward
1445 people have browsed it

PHP code: Method to display logged in user information

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

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

Let us understand this with some examples.

Example 1

In this example, we will create a login/logout system where the user will be authenticated after logging in and will be redirected to the dashboard page with their information visible. The user can then log out from the dashboard to reset the session.

File name: 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>
Copy after login

File name: 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;
?>
Copy after login

File name: 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>
Copy after login

Example 2

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

File name: 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>
Copy after login

File name: 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;
?>
Copy after login

File name: 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>
Copy after login

in conclusion

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

The above is the detailed content of PHP code: Method to display logged in user information. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template