【发布时间】:2022-01-01 13:51:02
【问题描述】:
我有一个接受登录信息的会话,如果用户在我的数据库中有效,我将设置页面发布数组以及用户的其他数据,并将其显示在另一个页面上的另一个会话中。我的电子邮件和密码正在发送到下一个会话,但我的其余数据没有发送,我不知道为什么。我确实知道我已成功从数据库表中检索登录用户的数据,但是其他字段被发送到 post 然后进入下一个会话时存在一些问题。
我的登录页面
<?php
session_start();
if(isset($_POST["firstName"]))
{
$_SESSION["firstName"] = $_POST["firstName"];
}
if(isset($_POST["lastName"]))
{
$_SESSION["lastName"] = $_POST["lastName"];
}
if(isset($_POST["phone"]))
{
$_SESSION["phone"] = $_POST["phone"];
}
if(isset($_POST["email"]))
{
$_SESSION["email"] = $_POST["email"];
}
if(isset($_POST["sin"]))
{
$_SESSION["sin"] = $_POST["sin"];
}
if(isset($_POST["password"]))
{
$_SESSION["password"] = $_POST["password"];
header('Location: ViewAllEmployees.php');
exit;
}
require "MySQLConnectionInfo.php";
$error = "";
// if email and password not set
if (! isset($_POST["email"]) || ! isset($_POST["password"])) {
$error = "Please enter employee login information.";
} else {
// if email and password set from login input
if ($_POST["email"] != "" && $_POST["password"] != "") {
try {
$pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);
// set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully" . "</br>";
$sqlQuery = "SELECT * FROM employee";
try {
$result = $pdo->query($sqlQuery);
$rowCount = $result->rowCount();
if ($rowCount == 0)
echo "There are no rows to display from the Employee table ";
// find employee
for ($i = 0; $i < $rowCount; $i++) {
$row = $result->fetch();
$firstname = $row[1];
$lastname = $row[2];
$email = $row[3];
$phone = $row[4];
$sin = $row[5];
$password = $row[6];
if (isset($_POST["email"]) && isset($_POST["password"])) {
if ($email == $_POST["email"] && $password == $_POST["password"]) {
$_POST["firstName"] = $firstname;
$_POST["lastName"] = $lastname;
$_POST["email"] = $email;
$_POST["phone"] = $phone;
$_POST["sin"] = $sin;
$_POST["password"] = $password;
break;
}
}
}
} catch (PDOException $e) {
echo "Login unsuccessful. " . $e->getMessage();
}
$pdo = null;
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
} else
$error = "Please enter employee login information.";
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
include "Header.php";
include "Menu.php";
?>
<div class="content">
<form action="Login.php" method="post">
Email Address: <input type="text" name="email" /> <br /> Password: <input
type="text" name="password" /> <br /> <br /> <input type="submit"
value="Login" />
</form>
<br /> <br />
</div>
<?php
echo $error;
include "Footer.php";
?>
</body>
</html>
【问题讨论】:
-
最好不要尝试这种方法。相反,通过登录验证用户,设置 cookie/会话变量以使他们保持登录状态,然后重定向。在随后的页面上,测试一个有效的授权会话,然后检索您想要的数据并将其显示在那里。不要尝试覆盖 $_POST 全局
标签: php html session post session-variables