How to create admin login page using PHP?

Here, we will have our administrator login page. Where the administrator definitely knows it’s username and secret key. Along these lines, our primary point here is to ensure administrator enter right information. Along these lines, for that, we will take help of the data set. Where we set an inquiry assuming Admin enters the username and secret key that is as of now set in Database, then, at that point, just administrator can go to the fundamental page of the administrator board. Else, be in total agreement.

we will assemble an enrollment framework that monitors which clients are administrator and which are ordinary clients. The typical clients in our application are not permitted to get to administrator pages. All clients (Admins just as expected clients) utilize a similar structure to login. In the wake of signing in, the typical clients are diverted to the list page while the administrator clients are diverted to the administrator pages.

How to create admin login page using PHP?

So how about we start with making the records, will we? Explore to the envelope on your machine that is available to the server (that is, htdocs assuming you are utilizing xampp and www in the event that you’re utilizing wampp), and make the accompanying documents and organizers:

<!DOCTYPE html>
<html>
<head>
	<title>Registration system PHP and MySQL</title>
</head>
<body>
<div class="header">
	<h2>Register</h2>
</div>
<form method="post" action="register.php">
	<div class="input-group">
		<label>Username</label>
		<input type="text" name="username" value="">
	</div>
	<div class="input-group">
		<label>Email</label>
		<input type="email" name="email" value="">
	</div>
	<div class="input-group">
		<label>Password</label>
		<input type="password" name="password_1">
	</div>
	<div class="input-group">
		<label>Confirm password</label>
		<input type="password" name="password_2">
	</div>
	<div class="input-group">
		<button type="submit" class="btn" name="register_btn">Register</button>
	</div>
	<p>
		Already a member? <a href="login.php">Sign in</a>
	</p>
</form>
</body>
</html>

That doesn’t look cool. We should take care of business.

Add a connection to the css record right under the <title></title> tag in the head part of the register.php document. Like so:

<link rel="stylesheet" href="style.css">

Then, at that point, open up style.css document and let out this css code in it:

* { margin: 0px; padding: 0px; }
body {
	font-size: 120%;
	background: #F8F8FF;
}
.header {
	width: 40%;
	margin: 50px auto 0px;
	color: white;
	background: #5F9EA0;
	text-align: center;
	border: 1px solid #B0C4DE;
	border-bottom: none;
	border-radius: 10px 10px 0px 0px;
	padding: 20px;
}
form, .content {
	width: 40%;
	margin: 0px auto;
	padding: 20px;
	border: 1px solid #B0C4DE;
	background: white;
	border-radius: 0px 0px 10px 10px;
}
.input-group {
	margin: 10px 0px 10px 0px;
}
.input-group label {
	display: block;
	text-align: left;
	margin: 3px;
}
.input-group input {
	height: 30px;
	width: 93%;
	padding: 5px 10px;
	font-size: 16px;
	border-radius: 5px;
	border: 1px solid gray;
}
#user_type {
	height: 40px;
	width: 98%;
	padding: 5px 10px;
	background: white;
	font-size: 16px;
	border-radius: 5px;
	border: 1px solid gray;
}
.btn {
	padding: 10px;
	font-size: 15px;
	color: white;
	background: #5F9EA0;
	border: none;
	border-radius: 5px;
}
.error {
	width: 92%; 
	margin: 0px auto; 
	padding: 10px; 
	border: 1px solid #a94442; 
	color: #a94442; 
	background: #f2dede; 
	border-radius: 5px; 
	text-align: left;
}
.success {
	color: #3c763d; 
	background: #dff0d8; 
	border: 1px solid #3c763d;
	margin-bottom: 20px;
}
.profile_info img {
	display: inline-block; 
	width: 50px; 
	height: 50px; 
	margin: 5px;
	float: left;
}
.profile_info div {
	display: inline-block; 
	margin: 5px;
}
.profile_info:after {
	content: "";
	display: block;
	clear: both;
}

This css code will be utilized for styling our whole application.

On the off chance that we invigorate the register.php page on the program, we get this excellence:

What we need presently is for the client to fill the structure and press the register button so the information can be saved in the data set. So we continue on to the following stage.

How about we make an information base called multi_login. In multi_login data set, make a table called clients with the accompanying fields:

id – int(10)
username – varchar(100)
email – varchar(100)
user_type – varchar(100)
secret key – varchar(100)
That is all we want for our information base.

We should move over to our register.php record by and by and do a few changes.

First we should ensure the structure’s strategy quality is set to post and that the activity characteristic is set to register.php implying that when the register button is clicked, the structure esteems are submitted to a similar page.

How about we presently compose the code to get these qualities and stores them in the information base. However, it is my custom to keep away from, however much as could be expected, stirring up php code in html so I’ll feel free to make a functions.php record to put this code inside and afterward make this code accessible in the register.php document.

At the extremely top (first line) of register.php document, add this line of code:

<?php include('functions.php') ?>
//...

Likewise, we need that when the client doesn’t enter the structure esteems accurately, blunder messages ought to be shown directing them to do it accurately. In the equivalent register.php document, just later the opening <form> tag, add this code:

<form method="post" action="register.php">
	<?php echo display_error(); ?>
//...
</form>

The display_error() is a basic capacity we will characterize without further ado.

Something final in the register.php record: Modify the username and email input fields by setting their worth credits to relating factors. Like so:

<input type="text" name="username" value="<?php echo $username; ?>">
<input type="email" name="email" value="<?php echo $email; ?>">

We’ll characterize the $username and $email factors soon…

Presently open up the void functions.php record and add this code in it:

<?php 
session_start();

// connect to database
$db = mysqli_connect('localhost', 'root', '', 'multi_login');

// variable declaration
$username = "";
$email    = "";
$errors   = array(); 

// call the register() function if register_btn is clicked
if (isset($_POST['register_btn'])) {
	register();
}

// REGISTER USER
function register(){
	// call these variables with the global keyword to make them available in function
	global $db, $errors, $username, $email;

	// receive all input values from the form. Call the e() function
    // defined below to escape form values
	$username    =  e($_POST['username']);
	$email       =  e($_POST['email']);
	$password_1  =  e($_POST['password_1']);
	$password_2  =  e($_POST['password_2']);

	// form validation: ensure that the form is correctly filled
	if (empty($username)) { 
		array_push($errors, "Username is required"); 
	}
	if (empty($email)) { 
		array_push($errors, "Email is required"); 
	}
	if (empty($password_1)) { 
		array_push($errors, "Password is required"); 
	}
	if ($password_1 != $password_2) {
		array_push($errors, "The two passwords do not match");
	}

	// register user if there are no errors in the form
	if (count($errors) == 0) {
		$password = md5($password_1);//encrypt the password before saving in the database

		if (isset($_POST['user_type'])) {
			$user_type = e($_POST['user_type']);
			$query = "INSERT INTO users (username, email, user_type, password) 
					  VALUES('$username', '$email', '$user_type', '$password')";
			mysqli_query($db, $query);
			$_SESSION['success']  = "New user successfully created!!";
			header('location: home.php');
		}else{
			$query = "INSERT INTO users (username, email, user_type, password) 
					  VALUES('$username', '$email', 'user', '$password')";
			mysqli_query($db, $query);

			// get id of the created user
			$logged_in_user_id = mysqli_insert_id($db);

			$_SESSION['user'] = getUserById($logged_in_user_id); // put logged in user in session
			$_SESSION['success']  = "You are now logged in";
			header('location: index.php');				
		}
	}
}

// return user array from their id
function getUserById($id){
	global $db;
	$query = "SELECT * FROM users WHERE id=" . $id;
	$result = mysqli_query($db, $query);

	$user = mysqli_fetch_assoc($result);
	return $user;
}

// escape string
function e($val){
	global $db;
	return mysqli_real_escape_string($db, trim($val));
}

function display_error() {
	global $errors;

	if (count($errors) > 0){
		echo '<div class="error">';
			foreach ($errors as $error){
				echo $error .'<br>';
			}
		echo '</div>';
	}
}	

Simple right?

Assuming you notice acutely you would now be able to see the distinction between a client and an administrator. In the register() work, the client is being saved as administrator if the user_type variable was sent in the post solicitation or as client, if no user_type was sent. Since our structure doesn’t have any field for the user_type, clearly the client we are making will be saved as client and not administrator.

At the point when a client is enlisted, we triumph ultimately the last embedded (id of the enrolled client) and log them in. From the client’s id, we can get the wide range of various traits of the client utilizing the getUserById() work. In the wake of getting the client, we put them in the meeting variable as an exhibit called client.

Putting away the client in a meeting variable implies that the client is accessible regardless of whether you invigorate and explore to different pages (where meeting has been begun). The client variable in the meeting doesn’t get lost; it must be lost by unsetting it (this is the manner by which we log the client out. Coming soon…).

Presently back to our enlistment structure, you notice that when you input qualities and snap the register button, you are diverted to the index.php page. Yet, it’s clear. So how about we make it appear as though a record page.

Open up index.php record in your content tool and put the accompanying code in it:

<?php 
	include('functions.php');
?>
<!DOCTYPE html>
<html>
<head>
	<title>Home</title>
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
	<div class="header">
		<h2>Home Page</h2>
	</div>
	<div class="content">
		<!-- notification message -->
		<?php if (isset($_SESSION['success'])) : ?>
			<div class="error success" >
				<h3>
					<?php 
						echo $_SESSION['success']; 
						unset($_SESSION['success']);
					?>
				</h3>
			</div>
		<?php endif ?>
		<!-- logged in user information -->
		<div class="profile_info">
			<img src="images/user_profile.png"  >

			<div>
				<?php  if (isset($_SESSION['user'])) : ?>
					<strong><?php echo $_SESSION['user']['username']; ?></strong>

					<small>
						<i  style="color: #888;">(<?php echo ucfirst($_SESSION['user']['user_type']); ?>)</i> 
						<br>
						<a href="index.php?logout='1'" style="color: red;">logout</a>
					</small>

				<?php endif ?>
			</div>
		</div>
	</div>
</body>
</html>

Also Read: A Step-By-Step Guide: sum() function in Python

Leave a Reply

Your email address will not be published.