I get the following errors when trying to register a user at my web page:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
^ Pointing towards PHP file with the registration script
And Internal Server Error pointing at the following script:
$("#login-submit").on('click', function () {
$.ajax({
url: 'ext/login_process.php',
type: "POST",
data: $("#login-form").serialize(),
success: function (result) {
if (result === "done") {
window.location.href = "index.php";
} else {
var elementExists = document.getElementById("msg-default");
if (elementExists !== null) {
document.getElementById("msg-default").innerHTML = "";
}
document.getElementById("err-msg").innerHTML = "<div class = 'alert alert-danger text-uppercase'><strong>Error! </strong>" + result + "</div></div>";
}
},
error: function (xhr, resp, text) {
console.log(xhr, resp, text);
}
});
});
The error occurs when pressing the register button. It works as normal if the username is already taken, then that message is displayed as it should.
The HTML:
<form name="register-form" id="register-form" method="POST">
<h5 class="text-center">Register</h5>
<div class="form-group">
<input type="text" class="form-control" placeholder="Username" name="username" required>
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Password" name="password" required>
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Repeat password" name="c-password" required>
</div>
<div class="form-group">
<label>I am...</label>
<div class="radio">
<label><input type="radio" name="position" checked value="s"> Student</label>
</div>
<div class="radio">
<label><input type="radio" name="position" value="t"> Teacher</label>
</div>
</div>
<div class="err-msg" id="err-msg"></div>
</form>
<button id="register-submit" class="btn btn-success">Senda</button>
PHP:
$username = trim(preg_replace('/\s+/', ' ', htmlspecialchars($_POST["username"])));
$password = htmlspecialchars($_POST["password"]); // Do not trim the password, let the user input whatever they desire
$confirmPssword = htmlspecialchars($_POST["c-password"]); // Do not trim the password, let the user input whatever they desire
$position = trim(preg_replace('/\s+/', ' ', htmlspecialchars($_POST["position"])));
if (empty($username))
{
echo "Invalid username!";
} else
if (empty($password))
{
echo "Invalid password!";
} else
if ($confirmPssword != $password)
{
echo "Password/Confirm Password didn't match!";
} else {
$sql = get_user_details($username);
$result = $conn->query($sql);
if ($result)
{
if ($result->num_rows > 0)
{
$result->close();
echo "username is already registered";
} else {
// Generate a new random salt
$salt = '$1$' . substr(strtr(base64_encode(random_bytes(32)), '+', '.'), 0, 8) . '$';
// Generate the MD5 hashed password
$cryptedPassword = crypt($password, $salt);
$sql = add_user($username, $cryptedPassword, $position);
if ($conn->query($sql))
{
echo "done";
} else {
echo "please refresh the page and try again";
}
}
} else {
echo "please refresh the page and try again";
}
$conn->close();
}
I am using this: https://code.jquery.com/jquery-3.1.1.min.js
Where can I get more specific information about the error on cpanel?
The network tab:
Server
nginx/1.14.1
Date
Mon, 24 Jun 2019 14:44:51 GMT
Content-Type
text/html; charset=UTF-8
Content-Length
0
Connection
keep-alive
Expires
Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control
no-store, no-cache, must-reval…te, post-check=0, pre-check=0
Pragma
no-cache
Request headers (542 B)
Host
(WEBSITE)
User-Agent
Mozilla/5.0 (Windows NT 10.0; …Firefox/56.0 Waterfox/56.2.11
Accept
*/*
Accept-Language
en-US,en;q=0.5
Accept-Encoding
gzip, deflate
Referer
http://(WEBSITE)/register.php
Content-Type
application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With
XMLHttpRequest
Content-Length
53
Cookie
PHPSESSID=v1qp6qo01igqlho8668sch2mu5
Connection
keep-alive
Pragma
no-cache
Cache-Control
no-cache
Nothing is displayed under "Response"