首先,我已经浏览了整个网站,并且无法找到适用于我的计划的解决方案。
这样说,我正在尝试创建一个启动会话的身份验证页面,并将登录页面中的用户名保存为会话变量。然后,我希望我的第三页检索用户名会话变量。
这是我的表格:
<html>
<head>
<title>Form Test</title>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<h1 align="center">Log in with your username and</h1>
<form class="form-horizontal" method="POST" action="formauth.php">
<div class="form-group">
<div class="control-label col-sm-3">
<label for="card">Username:</label>
</div>
<div class="col-sm-5">
<input type="text" name="username" placeholder="enter username here">
</div>
</div>
<div class="form-group">
<div class="form-group">
<div class="control-label col-sm-3">
<label for="card">Password:</label>
</div>
<div class="col-sm-5">
<input type="text" name="password" placeholder="enter password here">
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-3">
<div name="buttons" class="col-sm-offset-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</body>
</html>
这是我的身份验证页面。出于某种原因,我无法将此输入保存为输入到表单中的用户名作为会话变量。
<?php
session_start();
$_SESSION['username'] =$user_name;
if (isset($_POST['username']) &&
isset($_POST['password']))
{
$user = $_POST['username'];
$pwd = $_POST['password'];
require_once "login.php";
$conn = new mysqli($hn, $un, $pwd, $db);
if ($conn->connect_error) die($conn->connect_error);
$query = "Select Password from Users where Username = '$user'";
$result = $conn->query($query);
if (!$result) {
$result->close();
$conn->close();
header('Location: http://localhost/FormAuth/loginForm.html');
}
else {
$rows = $result->num_rows;
$result->data_seek(0);
$p=$result->fetch_assoc()['password'];
if ($p == $password) {
header("Location: http://localhost/FormAuth/afterlogin.php");
}
else {
$result->close();
$conn->close();
$result->close();
$conn->close();
header('Location: http://localhost/loginForm.html');
}
}
$result->close();
$conn->close();
}
else
{
$result->close();
$conn->close();
header('Location: http://localhost/loginForm.html');
}
?>
现在,对于我的第三页,我无法检索用户名会话变量。
session_start();
if (!isset($_SESSION['username'])) {
echo "Username is: ".$_user_name['username'] . "<br>";
echo "Test, I made it here!";
}else{
echo "You not logged in.";
}
?>
<html>
<head>
</head>
<body>
</body>
</html>
这导致:
Username is:
Test, I made it here!
答案 0 :(得分:1)
您需要先检查是否设置了发布值。
<?php
session_start();
if (isset($_POST['username'], $_POST['password'])){
...
然后,只有在密码匹配时,才能在重定向之前设置session
变量$_POST['username']
。
if ($p == $password) {
$_SESSION['username'] =$user;
header("Location: http://localhost/FormAuth/afterlogin.php");
}
然后,您可以在使用$_SESSION['username']
的任何页面上访问您的设置会话变量session_start();
。以第三页为例,
session_start();
if(isset($_POST['username']) && !empty($_POST['username'])){
// proceed to account dashboard / your required page
} else {
// redirect to login page / index
}
答案 1 :(得分:1)
在检查数据库中的用户名和密码后,您需要在会话中设置用户名。
<?php
session_start();
if (isset($_POST['username']) &&
isset($_POST['password']))
{
$user = $_POST['username'];
$pwd = $_POST['password'];
require_once "login.php";
$conn = new mysqli($hn, $un, $pwd, $db);
if ($conn->connect_error) die($conn->connect_error);
$query = "Select Password from Users where Username = '$user'";
$result = $conn->query($query);
if (!$result) {
$result->close();
$conn->close();
header('Location: http://localhost/FormAuth/loginForm.html');
}
else {
$rows = $result->num_rows;
$result->data_seek(0);
$p=$result->fetch_assoc()['password'];
if ($p == $pwd) { // use $pwd instead of $password
$_SESSION['username'] = $user; // set username here
header("Location: http://localhost/FormAuth/afterlogin.php");
}
else {
$result->close();
$conn->close();
$result->close();
$conn->close();
header('Location: http://localhost/loginForm.html');
}
}
$result->close();
$conn->close();
}
else
{
$result->close();
$conn->close();
header('Location: http://localhost/loginForm.html');
}
?>
在第三部分中,在尝试显示之前检查您的用户名是否已在会话中设置:
if (isset($_SESSION['username'])) {
echo "Username is: ".$_SESSION['username'] . "<br>";
}
答案 2 :(得分:0)
你的问题来自这一行:
<?php
session_start();
$_SESSION['username'] = $user_name;
if (isset($_POST['username']) &&
isset($_POST['password']))
{
$user = $_POST['username'];
改为执行此操作:
<?php
session_start();
if (isset($_POST['username']) && isset($_POST['password'])) {
$user = $_POST['username'];
$_SESSION['username'] = $user;
编辑:第三页:
session_start();
if (isset($_SESSION['username'])) {
echo "Username is: ".$_SESSION['username'] . "<br>";
echo "Test, I made it here!";
} else {
echo "You not logged in.";
}
?>
祝你好运。