javascript - 使用标头功能重定向后显示错误消息
问题描述
我正在编写一个允许用户发布候选人的新网站!首先,他们需要连接才能访问候选人页面。我的问题是,如果连接输入不正确,我会将用户重定向到带有错误消息的连接页面,但是我的消息没有显示:/我基于这个主题但没有成功:/此外,我不想通过通过 url 显示消息
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>";
}
?>
登录.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>
解决方案
更改顺序,首先设置会话值,然后调用标头。例如。:
session_start();
$_SESSION['message'] = "<div class='alert alert-danger'>Please fill all the
indicated fields !</div>";
header('Location: login.php');
推荐阅读
- sql - SQL Server:选择具有相同付款ID但付款类型不同的记录
- node.js - 表单数据 POST 未在服务器中收到,但邮递员正在处理 RAW JSON
- python - 如何从 python 代码本地安装模块
- c - 在创建具有默认值的未使用变量之前,指向结构的指针不起作用
- azure - Azure 函数和 Azure KeyVault 通过服务端点进行通信
- javascript - 加载时出现svg动画问题
- ios - PDFDocument 未从 URL 加载文件
- c# - 从另一个项目调用类
- flutter - 扑。拦截来自父母的事件
- snowflake-cloud-data-platform - 如何过滤我在 Snowflake 中运行的查询类型?