Login attempts

How To Limit Login Attempt Using PHP And MySQL

Source code

config.php

								
									<?php
									$con=mysqli_connect("localhost","root","","loginattemp");
									// Check connection
									if (mysqli_connect_errno())
									{
										echo "Failed to connect to MySQL: " . mysqli_connect_error();
									}
									?>
								
							

index.php

								
									<?php
									session_start();
									include_once('config.php');
									$msg='';
									if(isset($_POST['submit'])){
										$time=time()-30;
										$ip_address=getIpAddr();
									// Getting total count of hits on the basis of IP
										$query=mysqli_query($con,"select count(*) as total_count from loginlogs where TryTime > $time and IpAddress='$ip_address'");
										$check_login_row=mysqli_fetch_assoc($query);
										$total_count=$check_login_row['total_count'];
									  //Checking if the attempt 3, or youcan set the no of attempt her. For now we taking only 3 fail attempted
										if($total_count==3){
											$msg="Too many failed login attempts. Please login after 30 sec";
										}else{
									    //Getting Post Values
											$username=$_POST['username'];
											$password=md5($_POST['password']);
									    // Coding for login
											$res=mysqli_query($con,"select * from user where username='$username' and  password='$password'");
											if(mysqli_num_rows($res)){
												$_SESSION['IS_LOGIN']='yes';
												mysqli_query($con,"delete from loginlogs where IpAddress='$ip_address'");

												echo "<script>window.location.href='dashboard.php';</script>";

											}else{
												$total_count++;
												$rem_attm=3-$total_count;
												if($rem_attm==0){
													$msg="Too many failed login attempts. Please login after 30 sec";
												}else{
													$msg="Please enter valid login details.<br/>$rem_attm attempts remaining";
												}
												$try_time=time();
												mysqli_query($con,"insert into loginlogs(IpAddress,TryTime) values('$ip_address','$try_time')");
											}
										}
									}

									// Getting IP Address
									function getIpAddr(){
										if (!empty($_SERVER['HTTP_CLIENT_IP'])){
											$ipAddr=$_SERVER['HTTP_CLIENT_IP'];
										}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
											$ipAddr=$_SERVER['HTTP_X_FORWARDED_FOR'];
										}else{
											$ipAddr=$_SERVER['REMOTE_ADDR'];
										}
										return $ipAddr;
									}
									?>
									<!DOCTYPE html>
									<html lang="en">
									<head>
										<meta charset="utf-8">
										<meta name="robots" content="noindex, nofollow">
										<title>Login Form</title>
										<meta name="viewport" content="width=device-width, initial-scale=1">
										<link href="css/bootstrap.css" rel='stylesheet' type='text/css' />
										
									</head>
									<body>
										<div class="container" >
											
											<center>
												<div class="card col-md-8" style="margin-top: 30px;">
													<h3 class="text-center  pt-3 pb-1">Login form</h3>
													<h6 class="text-center  pt-2 pb-2">Username(admin), Password(1234)</h6>
													<div id="login-row" class="row justify-content-center align-items-center">
														<div id="login-column" class="col-md-6">
															<div id="login-box" class="col-md-12">
																<form id="login-form" class="form" method="post">
																	<div class="form-group">
																		<input type="text" name="username" placeholder="Username" id="username" class="form-control" required>
																	</div>
																	<div>class="form-group">
																		<input  type="password" name="password" placeholder="Password" id="password" class="form-control" required>
																	</div>
																	<div class="form-group">
																		<input type="submit" name="submit" class="btn btn-info btn-md" value="Submit">
																	</div>
																	<div id="result" style="color: red;"><?php echo $msg?></div>
																</form>
															</div>
														</div>
													</div>
												</div>
											</center>
										</div>
										<script src="js/jquery.min.js"></script>
										<script src="js/bootstrap.min.js"></script>
									</body>
									</html>
								
							

dashboard.php

								
									<?php
									session_start();
									if(!isset($_SESSION['IS_LOGIN'])){
										?>
										<script>
											window.location.href='index.php';
										</script>
										<?php
									}else{
										?>
										<!DOCTYPE html>
										<html lang="en">
										<head>
											<meta charset="utf-8">
											<meta name="robots" content="noindex, nofollow">
											<title>Login Form</title>
											<meta name="viewport" content="width=device-width, initial-scale=1">
											<link href="css/bootstrap.css" rel='stylesheet' type='text/css' />
										</head>
										<body>
											<div class="container">
												<div> </div>
												<div class="  d-flex flex-row align-items-center justify-content-between">
													<div class="">
														<a href="logout.php"><button type="button" class="btn btn-sm btn-primary"><i class="fas fa-plus" ></i> Logout
														</button></a>
													</div>
												</div>
												<div> </div>
												<div class="row ">
													<table id="" class="table table-bordered">
														<thead>
															<tr>
																<th class="text-center">No</th>
																<th>Username</th>
																<th>Password</th>
															</tr>
														</thead>
														<tbody>
															<tr>
																<td class="text-center">1</td>
																<td>admin</td>
																<td class="text-center">1234</td>
															</tr>
														</tbody>
													</table>
												</div>
											</div>
										</body>
										<?php
									}
									?>
								
							
Download

Comments

Victor Crontica

This is awesome thank you

crowy Atukwase

Thank you so much code4berry, you really improved my skills

Leave a Comment:

You Might Also Like

hostel booking

Hostel booking management system
Learn More
.

car rental

Car rental management system in Php
Learn More
.

student details

Student details management system
Learn More
.

Tourism

Tourism management system in Php and Mysql
Learn More

Latest Tutorial

sweet alerts

How to delete table row using sweet alert2
Learn More

piechart

How to create piechart with Mysql data
Learn More

register and login

How to register and login in php
Learn More

edit data

How to edit Mysql data in modal using php
Learn More