Table of Contents
Tutorial: Display Username After Login in PHP with Source Code
In this tutorial, we will create a Display Username After Login using PHP. This code can display a username after the user login an account. The system uses the PHP POST method to call a specific function that can log in a user account using the SELECT query by validating the username and password if it exists. After login successful, the PHP SESSION will do its part by storing the user id temporarily, then querying it to the next page to display the user account user name.
We will be using PHP as a scripting language and interpreter that is mainly used on any web server, including xamp, wamp, etc. It is being used to any popular websites, and it has a modern technology that can be easily used by the next generation.
Getting Started:
First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the 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.
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.
<?php $conn = mysqli_connect("localhost", "root", "", "db_user_login"); if(!$conn){ die("Error: Failed to connect to database!"); } ?>
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
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a> </div> </nav> <div class="col-md-3"></div> <div class="col-md-6 well"> <h3 class="text-primary">PHP - Display Username After Login</h3> <hr style="border-top:1px dotted #ccc;"/> <div class="col-md-3"> <h6>Username: <span class="text-primary">johnny123</span></h6> <h6>Password: <span class="text-primary">john12</span></h6> </div> <div class="col-md-6"> <form method="POST" action="login.php"> <div class="form-group"> <label>Username</label> <input type="text" name="username" class="form-control" required="required"/> </div> <div class="form-group"> <label>Password</label> <input type="password" name="password" class="form-control" required="required"/> </div> <center><button name="login" class="btn btn-primary">Login</button></center> </form> </div> </div> </body> </html>
home.php
<!DOCTYPE html> <?php session_start(); if(!ISSET($_SESSION['user_id'])){ header('location:index.php'); } ?> <html lang="en"> <head> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a> </div> </nav> <div class="col-md-3"></div> <div class="col-md-6 well"> <h3 class="text-primary">PHP - Display Username After Login</h3> <hr style="border-top:1px dotted #ccc;"/> <div class="col-md-3"></div> <div class="col-md-6"> <h1>WELCOME:</h1> <?php require'conn.php'; $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `user_id`='$_SESSION[user_id]'") or die(mysqli_error()); $fetch = mysqli_fetch_array($query); echo "<h2 class='text-success'>".$fetch['username']."</h2>"; ?> <a href="logout.php">Logout</a> </div> </div> </body> </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
<?php require_once 'conn.php'; session_start(); if(ISSET($_POST['login'])){ $username = $_POST['username']; $password = $_POST['password']; $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `username` = '$username' AND `password` = '$password'") or die(mysqli_error()); $fetch = mysqli_fetch_array($query); $row = mysqli_num_rows($query); if($row > 0){ $_SESSION['user_id']=$fetch['user_id']; echo "<script>alert('Login Successfully!')</script>"; echo "<script>window.location='home.php'</script>"; }else{ echo "<script>alert('Invalid username or password')</script>"; echo "<script>window.location='index.php'</script>"; } }
logout.php
<?php session_start(); session_destroy(); header('location: index.php') ?>