PHP - Display Username After Login

Submitted by: 
Language: 
Visitors have accessed this post 145 times.

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 https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

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.
tut1

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");
  3.  
  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.
index.php

  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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Display Username After Login</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3">
  19. <h6>Username: <span class="text-primary">johnny123</span></h6>
  20. <h6>Password: <span class="text-primary">john12</span></h6>
  21. </div>
  22. <div class="col-md-6">
  23. <form method="POST" action="login.php">
  24. <div class="form-group">
  25. <label>Username</label>
  26. <input type="text" name="username" class="form-control" required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Password</label>
  30. <input type="password" name="password" class="form-control" required="required"/>
  31. </div>
  32.  
  33. <center><button name="login" class="btn btn-primary">Login</button></center>
  34. </form>
  35. </div>
  36. </div>
  37. </body>
  38. </html>

home.php

  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="<a href="https://sourcecodester.com">Sourcecodester</a>
  16. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  17. </a> </div>
  18. </nav>
  19. <div class="col-md-3"></div>
  20. <div class="col-md-6 well">
  21. <h3 class="text-primary">PHP - Display Username After Login</h3>
  22. <hr style="border-top:1px dotted #ccc;"/>
  23. <div class="col-md-3"></div>
  24. <div class="col-md-6">
  25. <h1>WELCOME:</h1>
  26. <?php
  27. require'conn.php';
  28.  
  29.  
  30. $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `user_id`='$_SESSION[user_id]'") or die(mysqli_error());
  31. $fetch = mysqli_fetch_array($query);
  32.  
  33. echo "<h2 class='text-success'>".$fetch['username']."</h2>";
  34. ?>
  35. <a href="logout.php">Logout</a>
  36. </div>
  37. </div>
  38. </body>
  39. </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.
login.php

  1. <?php
  2. require_once 'conn.php';
  3. if(ISSET($_POST['login'])){
  4. $username = $_POST['username'];
  5. $password = $_POST['password'];
  6.  
  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);
  10.  
  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. }
  19.  
  20. }
  21.  
  22. ?>

logout.php

  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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.