Secure system access analyzes of different Google Payoneer platforms among other financial platforms.
In the following analyzes I found these characteristics:
- On the first failed attempt it shows the captcha code.
- After 5 attempts the user's account is blocked for 15 minutes.
- When you have access whether or not the fault history exists, you must answer a security question to continue.
- Does not allow double logging.
Other References: create secure login script
I have managed to fix my old code.
Note: Please edit my question to avoid possible duplicate question.
The corrections:
It ip
will continue to be blocked, it will not affect anything because being blocked ip
will only show the captcha code.
I have created a new fail_attempt table with the following columns: something like my idea.
id_fail_attempt id_user attempt ip datetime time
1 1 5 ::1 2017-08-23 17:57:46 2017-08-23 18:12:46
The following table login_attempts
lists the failed attempts of theip
id ip attempts datetime
1 ::1 2 2017-08-23 17:57:46
The boardusers
id username email password lastname active
1 Hola hola@ Hola Hola 1
How can I insert in the table fail_attempt
the id
user, the failed attempts, the ip
time in which the user will be blocked access and how to control the double login of the same user in the same system.
Full PHP code
How do I add new features and create a secure system?
login.php
<?php
session_start();
$message="";
$captcha = true;
//
$con = @new mysqli('localhost', 'root', '', 'systemuser');
if(count($_POST)>0 && isset($_POST["vcode"]) && $_POST["vcode"]!=$_SESSION["vcode"]) {
$captcha = false;
$message = "Los caracteres escritos no coinciden con la palabra de verificación. Inténtalo de nuevo.";
}
$ip = $_SERVER['REMOTE_ADDR'];
//Bloqueamos la ip por un día
$result = mysqli_query($con,"SELECT * FROM failed_login WHERE ip='$ip' AND date BETWEEN DATE_SUB( NOW() , INTERVAL 1 DAY ) AND NOW()");
$row = mysqli_fetch_assoc($result);
//Obtenemos datos para comprar intentos y para resetear intentos por su ultimo fecha.
$failed_login_attempt = mysqli_real_escape_string($con,$row['attempts']);
//Liberamos memoria.
mysqli_free_result($result);
if(count($_POST)>0 && $captcha == true) {
$username = mysqli_real_escape_string($con, $_POST["username"]);
$password = mysqli_real_escape_string($con, $_POST["password"]);
$username = htmlentities($username);
$password = htmlentities($password);
$save_passw = sha1($password);
$sql = "SELECT * fROM users where username='$username' AND password='$save_passw' AND active='1' ";
$query = mysqli_query($con, $sql);
$rowU = mysqli_fetch_assoc($query);
$UsernamaDB = mysqli_real_escape_string($con, $rowU["username"]);
$passwordDB = mysqli_real_escape_string($con, $rowU["password"]);
if($failed_login_attempt <1) {
//Si es su primer intento fallido, incluimos el primer registro en la BD
$con->query("INSERT INTO failed_login (ip,attempts,date) VALUES ('$ip', 1, NOW())");
} else {
if($failed_login_attempt <2){
//En caso de ya estar en la BD, sacamos el valor y agregamos +1
$contador = $row['attempts'] + 1;
$con->query("UPDATE failed_login SET attempts='$contador', date=NOW() WHERE ip = '$ip'");
}
}
if (empty($_POST) === false) {
$username = $_POST['username']; $password = $_POST['password'];
if (empty($username) === true || empty($password) === true) {
$message = "Es necesario introducir un nombre de usuario y contraseña";
} elseif ($username != $UsernamaDB) {
$message = "El 'Usuario' que has introducido no coincide. ";
} elseif ($save_passw != $passwordDB) {
$message = "Tu 'Contraseña' introducido no coincide. ";
} elseif($save_passw == $passwordDB && $username == $UsernamaDB) {
$_SESSION["id_user"] = 1;
$con->query("DELETE FROM login_attempts WHERE ip = '$ip'");
}
}
}
if(isset($_SESSION["id_user"])) {
header("Location:http://localhost/index.php");
}
?>
<h1><?php if($message!="") { echo $message; } ?></h1>
<form name="frmUser" method="post" action="">
<input type="text" name="username" placeholder="Usuario">
<input type="password" name="password" placeholder="Contraseña">
<!-- captcha-->
<?php if (isset($failed_login_attempt) && $failed_login_attempt >= 1) { ?>
<br><img src="image.php" id="phoca-captcha"/>
<input name="vcode" type="text" placeholder="Codigo captcha">
<?php } ?>
<!-- fin-->
<input type="submit" value="Iniciar sesión" id="button-login">
</form>