Login Page in PHP, We will build the registration form for creating a new account by filling the form. We will create a table that will store the user information.
Step 1: MySql Create Table
phpMyAdmin is a free software tool written in PHP. It supports a wide range of operations on MySQL and MariaDB. Frequently used operations (managing databases, tables, columns, relations, indexes, users, permissions, etc) can be performed via the user interface, while you still have the ability to directly execute any SQL statement.
You can Read : How to Add Google Analytics Code in WordPress
Create the user’s table inside your MySQL database. Open PHPMyAdmin in browser i.e. http://localhost/phpmyadmin
Create the database :
The number of columns: How many columns you want to create in the table โusersโ.Create the table โusersโ inside your MySQL database โdemoโ.
Fill all the column details (name, data type, default value, length etc.) then save.
Step 2: MySQL Create Database Connection
After creating the table, we need to create the PHP script in order to connect to the MySQL database server. Letโs create a file named โconfig.phpโ and put the following code inside it.
DB_SERVER: Database server may be localhost or IP address.
DB_USERNAME: MySQL username for database connection.
DB_PASSWORD: MySQL password for database connection.
DB_NAME: Name of the database.
File Name: config.php
<?php
/* Database credentials. */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'demo');
/* Attempt to connect to MySQL database */
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}?>
Step 3: Registration form template
Letโs create another PHP file named โregister.phpโ and put the following code in it. This code will create a web form that allows users to register themselves.
This script will also generate errors if the user will submit the form without entering the value, or if the username already exists.
File Name: register.php
<?php
// Include config file require_once "config.php";
$username = $password = $confirm_password = ""; $username_err = $password_err = $confirm_password_err = "";
if(isset($_POST["register"]) && $_POST["register"]=="Register") { $username = trim($_POST["username"]); $password = trim($_POST["password"]);
if(empty(trim($_POST["username"]))) { $username_err = "Please enter a username."; }
else { $sql = "SELECT * FROM users WHERE username = '$username'"; $result = mysqli_query($conn,$sql); $user = mysqli_fetch_assoc($result);
if($user){ if($user['username'] === $username) { $username_err = "This username is already taken."; }}}
if(empty(trim($_POST["password"]))) { $password_err = "Please enter a password."; } if(empty(trim($_POST["confirm_password"]))) { $confirm_password_err = "Please confirm password."; } else{ $confirm_password = trim($_POST["confirm_password"]); if(empty($password_err) && ($password != $confirm_password)) { $confirm_password_err = "Password did not match."; }} if(empty($username_err) && empty($password_err) && empty($confirm_password_err)) { $password = md5($password); $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')"; $result = mysqli_query($conn,$sql); mysqli_close($conn); header('location: login.php'); }}?>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
body{ font: 14px sans-serif; }
.form-container{ width: 400px; padding: 20px; }
</style>
</head>
<body>
<div class="form-container">
<h3>Register</h3>
<form action="register.php" method="post">
<div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>"
<label>Username</label>
<input type="text" name="username" class="form-control" value="<?php echo $username;?>">
<span class="help-block"><?php echo $username_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
<label>Password</label>
<input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
<span class="help-block"><?php echo $password_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
<label>Confirm Password</label>
<input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
<span class="help-block"><?php echo $confirm_password_err; ?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-info" value="Register" name="register">
<input type="reset" class="btn btn-warning" value="Reset">
</div>
<p>Already a member? <a href="login.php">Login here</a>.</p>
</form>
</div>
</body>
</html>
Open registration page in browser i.e. http://localhost/demo/register.php
Login Page in PHP
In this section, we will build the login form in which the user enters their username and password. When the user submits the form, these inputs will be verified by the credentials stored in the database. If the username & password match, the user will be able to access the index page. Otherwise, login will be failed with proper error.
Step 4: Create a bootstrap login form
Create another PHP file named โlogin.phpโ and put the following code in it. This code will create a web form that allows users to log in themselves.
This script contains all types of Validation. It will generate errors if the user will submit the form without entering the value, or if username & password is incorrect.
File Name: login.php
<?php
session_start();
if(isset($_SESSION["username"])) {
header("location: index.php");
exit;
}
// Include config file require_once "config.php";
$username = $password = "";
$username_err = $password_err = "";
if(isset($_POST["login"]) && $_POST["login"]=="Login") {
$username = trim($_POST["username"]); if(empty(trim($_POST["username"]))) {
$username_err = "Please enter a username.";
}
else{
$username = trim($_POST["username"]);
}
if(empty(trim($_POST["password"]))){
$password_err = "Please enter a password.";
}
else{
$password = trim($_POST["password"]);
}
if(empty($username_err) && empty($password_err)){
$sql = "SELECT username,password FROM users WHERE username='$username'";
$result = mysqli_query($conn,$sql);
$user = mysqli_fetch_assoc($result);
if($user){
if($user['password'] === md5($password)){ session_start();
$_SESSION['username'] = $username;
header('location: index.php');
}
else{
$password_err = "Password is incorrect.";
}}
else{
$username_err = "Username does not exist.";
}
mysqli_close($conn);
}} ?>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css"><style type="text/css">
body{ font: 14px sans-serif; }
.form-container{ width: 400px; padding: 20px; }
</style>
</head>
<body>
<div class="form-container">
<h3>Login</h3>
<form action="login.php" method="post"><div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
<label>Username</label>
<input type="text" name="username" class="form-control" value="<?php echo $username;?>">
<span class="help-block"><?php echo $username_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
<label>Password</label>
<input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
<span class="help-block"><?php echo $password_err; ?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-info" value="Login" name="login"><input type="reset" class="btn btn-warning" value="Reset">
</div>
<p>Not yet a member? <a href="register.php">SignUp</a>.</p>
</form>
</div>
</body>
</html>
Login page in browser http://localhost/demo/login.php
Step 5: Creating the Index Page
Letโs create another PHP file named โindex.phpโ and put the following code in it. Here, the user will be redirected after a successful login.
index.php
<?php
session_start();
if(!isset($_SESSION["username"])){
header("location: login.php");
exit;
}?>
<html>
<head>
<title>Index</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
</head>
<body>
<div class="page-header">
<h3>Hi, <b><?php if (isset($_SESSION['username'])) : echo $_SESSION["username"]; endif ?></b>.Welcome In Developerhelps. </h3></div>
<p>
<a href="logout.php" class="btn btn-danger">Logout</a>
</p>
</body>
</html>
Open login page in browser i.e. http://localhost/demo/index.php
Step 6: Create a Logout Page
Finally, Letโs create a PHP file named โlogout.phpโ and put the following code in it. When the user clicks on the logout button, the session will be destroyed and redirect the user back to the login page.
File Name: logout.php
<?php
session_start();
$_SESSION = array();
session_destroy();
header("location: login.php");
exit;
?>
More Related Post
Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.
My spouse and I stumbled over here different web page and thought I may as well check things out. I like what I see so I am just following you. Look forward to finding out about your web page yet again.
Woah! I’m really digging the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between usability and visual appearance. I must say you have done an amazing job with this. Also, the blog loads super fast for me on Internet explorer. Excellent Blog!
Thanks for your feedback!!!
This post is great! I read your blog site fairly regularly, and you are always coming up with some decent stuff. I shared this blog post on my Facebook, and my followers loved it!
Good luck in the future.
Hello there! This is my first comment here so I just wanted to give
a quick shout out and tell you I truly enjoy reading through your blog posts.
Can you suggest any other blogs/websites/forums that go over the same subjects?
Thanks for your time!
Tาปis iั a great post! Really a shฮฟuโ ผd-rะตad and a discovery!
This actually helped me thank you.
This is a topic that’s near to my heart…
Many thanks! Where are your contact details though?
Great writeup! I shared your website on my Twitter.
Hopefully my visitors will like your articles as well. Good luck.
Thanks for sharing your thoughts about website.
Regards
Outstanding quest there. What occurred after? Thanks!
There iั obviโฒusly lots to learn about this subjeฯฒt.
I like all the points you made.
Hello it’s me, I am also visiting this website daily, this web page is genuinely good and the
viewers are actually sharing pleasant thoughts.
Everything is extremely open with a truly clear portrayal of the difficulties.
It was genuinely educational. Your site is useful.
Many thanks to you for sharing!
Thanks for your feedback!!
Keep this going please, great job!
Very Nice Post !!
Very good article.Many thanks again. Cool.
I truly appreciate this article. Really looking forward to reading more. Much obliged.