Mam poniższy kod logowania do panelu strony. Proszę o sprawdzenie poprawności pod kątem ewentualnych błędów. W pliku index.php mam input typu email więc dorobiłem blokowanie adresu IP w sesji jeżeli ktoś w przeglądarce zmieni typ inputa np. na name. Początkowo dałem
$_SESSION['blockIP']= "unset"
żeby kod nie rzucał błędów, ale nie jestem pewny czy jest do dobre rozwiązanie. Proszę o jakieś sugestie w którym kierunku iść, co jest potrzebne a co nie.
<?php
if ((!isset($_POST['email'])) || (!isset($_POST['pass']))) {
header('Location: index.php'); }
try
{
require_once "connect.php";
$connect = @new mysqli($host, $db_user, $db_password, $db_name);
if ($connect->connect_errno!=0)
{
header('Location: index.php'); }
else
{
$sekret = "*****************************************";
$check = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$sekret.'&response='.$_POST['g-recaptcha-response']); $replay = json_decode($check);
if ($replay->success==false)
{
$_SESSION['error'] = '<div class="error">Potwierdź, że nie jesteś robotem!</div>';
header('Location: index.php'); }
else
{
$email = $_POST['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{
$result = $connect->prepare("SELECT * FROM persons WHERE email=?");
$result -> bind_param('s', $email);
$result->execute();
$result_bind = $result->get_result();
$row = $result_bind->fetch_assoc();
if(!isset($_SESSION['blockIP'])) {
$_SESSION['blockIP']= "unset";
}
if(($result_bind->num_rows == 1
) && (password_verify
($_POST['pass'],$row['pass'])) && (strpos($_SERVER['REMOTE_ADDR'],$_SESSION['blockIP']) !== 0
)) {
$_SESSION['logged'] = true;
$_SESSION['id'] = $row['id'];
$_SESSION['email'] = $row['email'];
unset($_SESSION['blockIP']); unset($_SESSION['error']); $result->free_result();
if ($connect->query("INSERT INTO `sessions` (`id`,`email`,`date`,`ip`,`status`) VALUES (NULL, '$email', Now(), '{$_SERVER['REMOTE_ADDR']}', 'logged')"))
{
header('Location: panel.php');
}
}
else
{
$connect->query("INSERT INTO `sessions` (`id`,`email`,`date`,`ip`,`status`) VALUES (NULL, '$email', Now(), '{$_SERVER['REMOTE_ADDR']}', 'failed')");
$_SESSION['error'] = '<div class="error">Błędny login lub hasło!</div>';
header('Location: index.php'); }
}
else
{
$_SESSION['error'] = '<div class="error">Podaj poprawny adres e-mail!</div>';
$_SESSION['blockIP'] = $_SERVER['REMOTE_ADDR'];
header('Location: index.php'); }
}
$connect->close();
}
}
catch(Exception $e)
{
echo '<div class="alert"><div class="alert-danger">'.$e.'</div></div>'; }
?>