Skip to main content

PHP - Display Username After Login

In this tutorial we will create a Display Username After Login using PHP. This code can display username after the user login an account. The code use PHP POST method to call a specific function that can login a user account using SELECT query by validating the username and password if exist. After login successful the PHP SESSION will do its part by storing the user id temporary, then querying it to the next page in order to display the user account user name.

We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites and it has a modern technology that can easily be use by the next generation.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And, this is the link for the bootstrap that i used for the layout design

Creating Database

Open your database web server then create a database name in it db_user_login, after that click Import then locate the database file inside the folder of the application then click ok.

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "db_user_login");
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as shown below.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Display Username After Login</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3">
  18. <h6>Username: <span class="text-primary">johnny123</span></h6>
  19. <h6>Password: <span class="text-primary">john12</span></h6>
  20. </div>
  21. <div class="col-md-6">
  22. <form method="POST" action="login.php">
  23. <div class="form-group">
  24. <label>Username</label>
  25. <input type="text" name="username" class="form-control" required="required"/>
  26. </div>
  27. <div class="form-group">
  28. <label>Password</label>
  29. <input type="password" name="password" class="form-control" required="required"/>
  30. </div>
  32. <center><button name="login" class="btn btn-primary">Login</button></center>
  33. </form>
  34. </div>
  35. </div>
  36. </body>
  37. </html>


  1. <!DOCTYPE html>
  2. <?php
  3. if(!ISSET($_SESSION['user_id'])){
  4. header('location:index.php');
  5. }
  6. ?>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  10. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  11. </head>
  12. <body>
  13. <nav class="navbar navbar-default">
  14. <div class="container-fluid">
  15. <a class="navbar-brand" href="">Sourcecodester</a>
  16. </div>
  17. </nav>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6 well">
  20. <h3 class="text-primary">PHP - Display Username After Login</h3>
  21. <hr style="border-top:1px dotted #ccc;"/>
  22. <div class="col-md-3"></div>
  23. <div class="col-md-6">
  24. <h1>WELCOME:</h1>
  25. <?php
  26. require'conn.php';
  29. $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `user_id`='$_SESSION[user_id]'") or die(mysqli_error());
  30. $fetch = mysqli_fetch_array($query);
  32. echo "<h2 class='text-success'>".$fetch['username']."</h2>";
  33. ?>
  34. <a href="logout.php">Logout</a>
  35. </div>
  36. </div>
  37. </body>
  38. </html>

Creating the Main Function

This code contains the minor function of the application. This code will display the user account username after login. To do this just kindly write these block of codes inside the text editor, then save it as shown below.

  1. <?php
  2. require_once 'conn.php';
  3. if(ISSET($_POST['login'])){
  4. $username = $_POST['username'];
  5. $password = $_POST['password'];
  7. $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `username` = '$username' AND `password` = '$password'") or die(mysqli_error());
  8. $fetch = mysqli_fetch_array($query);
  9. $row = mysqli_num_rows($query);
  11. if($row > 0){
  12. $_SESSION['user_id']=$fetch['user_id'];
  13. echo "<script>alert('Login Successfully!')</script>";
  14. echo "<script>window.location='home.php'</script>";
  15. }else{
  16. echo "<script>alert('Invalid username or password')</script>";
  17. echo "<script>window.location='index.php'</script>";
  18. }
  20. }
  22. ?>


  1. <?php
  2. header('location: index.php')
  3. ?>

There you have it we successfully created a Display Username After Login using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.