首页 > 解决方案 > 使用标头功能重定向后显示错误消息

问题描述

我正在编写一个允许用户发布候选人的新网站!首先,他们需要连接才能访问候选人页面。我的问题是,如果连接输入不正确,我会将用户重定向到带有错误消息的连接页面,但是我的消息没有显示:/我基于这个主题但没有成功:/此外,我不想通过通过 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>

标签: javascriptphphtml

解决方案


更改顺序,首先设置会话值,然后调用标头。例如。:

session_start();
$_SESSION['message'] = "<div class='alert alert-danger'>Please fill all the 
indicated fields !</div>";
header('Location: login.php');

推荐阅读