I'm trying to make a login system for my website which I'll be releasing in public later on. I have inserted some main samples of my code in here and I would like some useful advise or suggestions. Also if I'm doing anything unsafe or anything which may cause problems in future please let me know. Thanks
login.inc.php:
<?php
error_reporting(0);
$error_message = "Website ran into an error";
session_start();
if (isset($_SESSION['user_id'])) {
exit();
}
$parameters = json_decode(file_get_contents('php://input'), true);
if (isset($parameters["inserted_id"]) && isset($parameters["inserted_password"])) {
// FOR TEST USE I'M USING xampp server, localhost
$connection = mysqli_connect("localhost", "root", "", "users") or die($error_message);
$inserted_id = $parameters['inserted_id'];
$inserted_password = $parameters['inserted_password'];
$statement = mysqli_stmt_init($connection) or die($error_message);
if (mysqli_stmt_prepare($statement, "SELECT id, account_name, password FROM user WHERE account_name=? OR email=?;")) {
mysqli_stmt_bind_param($statement, "ss", $inserted_id, $inserted_id);
mysqli_stmt_execute($statement) or die($error_message);
if (($row = mysqli_fetch_assoc(mysqli_stmt_get_result($statement))) == null) {
echo("No records found with inserted inputs");
mysqli_stmt_close($statement);
mysqli_close($connection);
exit();
} else {
if (password_verify($inserted_password, $row['password'])) {
$_SESSION['user_id'] = $row['id'];
$_SESSION['account_name'] = $row['account_name'];
echo("pass");
} else {
echo("Wrong password");
}
mysqli_stmt_close($statement);
mysqli_close($connection);
exit();
}
}
mysqli_close($connection);
exit();
}
?>
login.js:
var request_sent = false;
document.querySelector("button").onclick = function () {
if (!request_sent) {
var inserted_id = document.getElementById("inserted_id").value;
var inserted_password = document.getElementById("inserted_password").value;
if (inserted_id == "" || inserted_password == "") {
alert("Both fields need to be filed out");
} else {
request_sent = true;
http_request("login.inc.php", JSON.stringify({"inserted_id":inserted_id, "inserted_password":inserted_password}));
}
}
}
function http_request(url, params) {
$.ajax({
type: "POST",
url: url,
data: params,
success: function(data) {
if (data == "pass") {
window.open("../website", "_self", "", false);
} else {
alert(data);
}
request_sent = false;
}
});
}
login.html:
<html>
<head>
<title></title>
</head>
<body>
<input id="inserted_id" type="text" placeholder="Account Name/e-mail"/>
<input id="inserted_password" type="password" placeholder="Password"/>
<button>SUBMIT</button>
<script src="login.js"></script>
</body>
</html>