I have an AWS Lightsail LAMP stack (not WordPress) and coded a simple login/session to a subdirectory of the main site because it's just for me, and not for normal website users.
When I access the site with its static IP address, the login works properly and it redirects to home.php, but when I use the website's name instead, the redirect doesn't work and it just reloads index.php.
/backstage/index.php
<?php
ob_start();
$is_invalid = false;
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$mysqli = require __DIR__ . "/database.php";
$email = $mysqli->real_escape_string($_POST["email"]);
$password = $mysqli->real_escape_string($_POST["password"]);
$sql = sprintf("SELECT * FROM user
WHERE email = '%s'", $email);
$result = $mysqli->query($sql);
$user = $result->fetch_assoc();
if ($user) {
if (password_verify($password, $user["password_hash"])) {
session_start();
session_regenerate_id();
$_SESSION["user_id"] = $user["id"];
// if the user and password match, this should redirect
header("Location: home.php");
exit;
}else {
die("bad password");
exit;
}
}else {
die("bad user");
exit;
}
$is_invalid = true;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head>
<body>
<h1>Login</h1>
<?php if ($is_invalid): ?>
<em>Invalid login</em>
<?php endif; ?>
<form method="post">
<label for="email">email</label>
<input type="email" name="email" id="email"
value="<?= htmlspecialchars($_POST["email"] ?? "") ?>">
<label for="password">Password</label>
<input type="password" name="password" id="password">
<button>Log in</button>
</form>
</body>
</html>
This is what I see in the apache access_log:
x0.x76.x13.x07 - - [24/Feb/2023:21:42:57 +0000] "POST /backstage/index.php HTTP/1.1" 302 -
x4.x52.x3.x77 - - [24/Feb/2023:21:42:58 +0000] "GET /backstage/home.php HTTP/1.1" 302 -
Also, there's nothing fancy in the DNS records, just a CNAME record that points www traffic to the non-www page.
Is this a PHP error, a DNS problem, a CloudFront issue, or something else completely?