apache PHP登录重定向适用于IP地址,但不适用于网站名称

fjnneemd  于 2023-03-09  发布在  Apache
关注(0)|答案(1)|浏览(123)

我有一个AWS Lightsail LAMP堆栈(不是WordPress),并编写了一个简单的登录/会话到主站点的子目录,因为它只是为我,而不是为普通网站用户。
当我使用静态IP地址访问网站时,登录正常,它重定向到home.php,但当我使用网站名称时,重定向不起作用,它只是重新加载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>

这是我在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 -

此外,DNS记录中没有什么花哨的东西,只是一个CNAME记录,它将www流量指向非www页面。
这是一个PHP错误,DNS问题,CloudFront问题,还是其他完全不同的问题?

ckx4rj1h

ckx4rj1h1#

原来是CloudFront的问题。CloudFront没有转发cookie。所以我修改该高速缓存设置,也转发cookie,这样就修复了它。

相关问题