Php AWS应用程序负载平衡器无法通过Apache/MySQL数据库处理登录表单请求
我已经部署了一个部署了Apache的AWS EC2实例,该实例承载一个简单的注册/登录表单。当用户注册时,他们的凭据存储在部署在辅助AWS EC2实例上的MySQL数据库中。因此,当用户点击登录页面时,他们输入他们的凭据,PHP代码用MySQL DB验证这些凭据,然后允许用户输入 此设置部署在两个AZ中,MySQL数据库是主复制的 我想在两个AWS EC2(Apache)web服务器前面放置一个应用程序负载平衡器,以解释其中一个AZ中的故障。但是,当我部署AWS应用程序负载平衡器并使用DNS名称登录时,应用程序似乎不会验证用户凭据,并从表单中删除用户名/密码(几乎就像刷新页面一样) 该应用程序是用PHP构建的,非常简单,但我无法解释为什么使用负载平衡器时我的应用程序不工作 有什么建议吗 我的身份验证方法:Php AWS应用程序负载平衡器无法通过Apache/MySQL数据库处理登录表单请求,php,mysql,amazon-web-services,apache,amazon-ec2,Php,Mysql,Amazon Web Services,Apache,Amazon Ec2,我已经部署了一个部署了Apache的AWS EC2实例,该实例承载一个简单的注册/登录表单。当用户注册时,他们的凭据存储在部署在辅助AWS EC2实例上的MySQL数据库中。因此,当用户点击登录页面时,他们输入他们的凭据,PHP代码用MySQL DB验证这些凭据,然后允许用户输入 此设置部署在两个AZ中,MySQL数据库是主复制的 我想在两个AWS EC2(Apache)web服务器前面放置一个应用程序负载平衡器,以解释其中一个AZ中的故障。但是,当我部署AWS应用程序负载平衡器并使用DNS名称
<?php
session_start();
// Change this to your connection info.
$DATABASE_HOST = 'XXX.XX.XX.XX';
$DATABASE_USER = 'XXX';
$DATABASE_PASS = 'XXX';
$DATABASE_NAME = 'XXX';
// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
// If there is an error with the connection, stop the script and display the error.
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
// Could not get the data that should have been sent.
exit('Please fill both the username and password fields!');
}
// Prepare our SQL
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
// Store the result so we can check if the account exists in the database.
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
// Account exists, now we verify the password.
if ($_POST['password'] === $password) {
// Verification success! User has logged-in!
// Create sessions, so we know the user is logged in, they basically act like cookies but remember the data on the server.
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
// echo 'Welcome ' . $_SESSION['name'] . '!';
header('Location: home.php');
} else {
// Incorrect password
echo 'Incorrect username and/or password!';
}
} else {
// Incorrect username
echo 'Incorrect username and/or password!';
}
$stmt->close();
}
?>
在没有分布式会话存储或启用粘性会话的情况下,不能有多个需要用户登录的web服务器。现在,您的用户会话在多个服务器之间来回跳转,但您只登录到其中一个服务器
解决此问题的最简单方法是进入负载平衡器设置并暂时启用粘性会话,这样用户的所有请求都将“粘贴”到单个服务器。是否配置了分布式会话存储?如果没有,您是否在负载平衡器上启用了粘滞会话?没有@MarkB,据我所知,我没有分发会话存储,也没有启用粘滞会话。Apache中使用的配置是默认配置(即,我没有更改任何内容)。