I had built a login system (with registration function) and tested it on my server (separate computer on same network) and it all worked fine.
Then I copied the code to my main computer to use it on localhost. Apache server and MySQL server was done throught XAMPP.
When entering the credentials and clicking submit, chrome answers with a HTTP 405 error: HTTP Method not allowed. It seems to be a problem with the method in the form, POST, but I have no clue what. I have checked the apache error log in XAMPP but without success. Nothing had been logged at the time of problem occurence
HTML (only the form part with no CSS):
<body>
<div class="login">
<h1>Login</h1>
<form action="authenticate.php" method="post">
<label for="username">
<i class="fas fa-user"></i>
</label>
<input type="text" name="username" placeholder="Username" id="username" required>
<label for="password">
<i class="fas fa-lock"></i>
</label>
<input type="password" name="password" placeholder="Password" id="password" required>
<input type="submit" value="Login">
</form>
</div>
</body>
PHP (The authenticate.php file):
<?php
session_start();
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'users';
$conn = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
if ( !isset($_POST['username'], $_POST['password']) ) {
exit('Please fill both the username and password fields!');
}
if ($stmt = $conn->prepare('SELECT id, password FROM accounts WHERE username = ?')){
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
$stmt->store_result();
}
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
if (password_verify($_POST['password'], $password)) {
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
header('Location: home.php');
} else {
echo 'Incorrect password!.';
}
} else {
echo 'Incorrect username and/or password!';
}
$stmt->close();
?>