I have made a log in form for my project. The error message is being displayed when I'm on localhost, but it does not show up when I upload it to the server, instead the page refreshes when Log in button gets clicked.
What am I doing wrong?
Here is the source:
//This is the logic. This passes the data to the auth.php page (see code below)
<?php
require 'database.php';
require 'constants.php';
if (isset($_POST['username']) && isset($_POST['password'])) {
function validate($con){
$con = trim($con);
$con = stripslashes($con);
$con = htmlspecialchars($con);
return $con;
}
$username = validate($_POST['username']);
$pass = validate($_POST['password']);
//If username/password field is empty -> error message
if (empty($username)) {
header("Location: " . ROOT_URL . "auth.php?error=Username is needed");
exit();
}else if(empty($pass)){
header("Location: " . ROOT_URL . "auth.php?error=Password is needed");
exit();
}else{
$result = $con->query("SELECT * FROM users WHERE username='$username' AND password='$pass'");
//If username/password fields are correct/wrong -> move to index page/error message
if ($result->num_rows === 1) {
$row = $result->fetch_assoc();
if ($row['username'] === $username && $row['password'] === $pass) {
$_SESSION['username'] = $row['username'];
header("Location: " . ROOT_URL);
exit();
}else{
header("Location: " . ROOT_URL . "auth.php?error=Wrong username or password!");
exit();
}
}else{
header("Location: " . ROOT_URL . "auth.php?error=Wrong username or password!");
exit();
}
}
}else{
header("Location: " . ROOT_URL . "auth.php");
exit();
}
Here is frontend part with the error feedback:
<?php
//Getting the database informations
require 'config/database.php';
?>
//Getting the information from the code above shown
<form action="success-auth.php" method="POST"> <!-- <?= ROOT_URL ?> -->
<header>
<div class="main-header">
//The error message, which only works in localhost
<?php if (isset($_GET['error'])) { ?>
<p class="alert__message error"><?php echo $_GET['error']; ?></p>
<?php } ?>
<div class="inp">
<input type="text" name="username" placeholder="Username">
</div>
<div class="inp">
<input type="password" id="btn" name="password" placeholder="Password">
</div>
<p><button type="submit" name="submit">Log in</button></p>
</div>
</header>
</form>
I can't find what the issue is. Any kind of help is appreciated!