我正在编码一个新站点,允许用户发布候选职位! 首先,他们需要连接才能访问候选人页面。 我的问题是,如果连接输入不正确,则会将用户重定向到带有错误消息的连接页面,但是我的消息未显示:/ 我以这个主题为基础,但未成功:/ 此外,我不想通过网址显示消息
displaying a message after redirecting the user to another web page
checkForm.php
<?php
require('../config/config.php');
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if(isset($_POST['identity']) && isset($_POST['password']))
{
$username = $_POST['identity'];
$password = $_POST['password'];
$stmt = $cnx->prepare("SELECT * from users WHERE nameUser = '".$username."' AND passwordUser = '".sha1($password)."'");
$stmt->execute();
$users = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($users);
$result = $stmt->rowCount();
if($result > 0)
{
session_start();
$_SESSION['user'] = $_POST['identity'];
$_SESSION['email'] = $users['emailUser'];
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + (120 * 60);
$_SESSION['pwd'] = $_POST['password'];
$_SESSION['id'] = $users['userId'];
header('Location: ../index.php');
}
else
{
header('Location: login.php');
//echo "<div class='alert alert-warning'>Connection failed ! Please check your information !</div>";
session_start();
$_SESSION['message'] = "<div class='alert alert-warning'>Connection failed ! Please check your information !</div>";
}
}
}
else
{
header('Location: login.php');
session_start();
$_SESSION['message'] = "<div class='alert alert-danger'>Please fill all the indicated fields !</div>";
}
?>
login.php
<?php
include('../index.php');
include('../template.php');
?>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
</head>
<body>
<form method="post" action="../templates/checkForm.php">
<div class="container">
<?php
session_start();
if(isset($_SESSION['message']))
{
echo $_SESSION['message'];
unset($_SESSION['message']);
}
?>
<div class="card card-container">
<img id="profile-img" class="profile-img-card" src="../img/avatar.png" />
<p id="profile-name" class="profile-name-card"></p>
<form class="form-signin">
<span id="formIdentity" class="formIdentity"></span>
<input type="text" id="identity" name="identity" class="form-control" placeholder="Your identity" autofocus>
<input type="password" id="password" name="password" class="form-control" placeholder="Your password">
<button class="btn btn-lg btn-primary btn-block btn-signin" type="submit" id="submitBtn" name="submit" onclick="checkForm(this)">Connect</button>
</form><!-- /form -->
<a href="../templates/forgotPassword.php" class="forgot-password">
forgot Password ?
</a>
</div><!-- /card-container -->
</div><!-- /container -->
</form>
</body>
</html>
答案 0 :(得分:0)
更改顺序,首先设置会话值,然后调用标题。例如:
session_start();
$_SESSION['message'] = "<div class='alert alert-danger'>Please fill all the
indicated fields !</div>";
header('Location: login.php');