login system via on localhost not working

Viewed 880

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();
?>
1 Answers

HTTP 405 error: HTTP Method not allowed

First thing of note here is that you have presented a lot of code here, most of which is irrelevant to the issue you reported. If you read the FAQ you'll see that you should be providing a minimal reproducable example.

Your code does not produce this error, it's your webserver doing this. So your MRE is as simple as:

<html>
<?php print "PHP is executing<br />"; ?>
<form method='POST'><input type='submit' value='go'></form>
</html> 

Webserver config is more appropriately discussed on ServerFault then here. I suggest you confirm you can replicate the error with the MRE here and post a question there with the code, error message, log entries and the relecvant parts of your webserver config.

Related