dtt83024 2018-08-08 07:21
浏览 110
已采纳

使用标题功能重定向后显示错误消息

I'm coding a new site allowing users to post candidatures ! First, They need to connect in order to access at candidature page. My problem is that if connection input are incorrect, I redirect the user to the connection page with an error message, however my message is not displaying :/ I based myself on this subject but unsuccessfully :/ Furthermore, I don't want to pass by the url to show the message

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>
  • 写回答

1条回答 默认 最新

  • duanfu2562 2018-08-08 07:41
    关注

    Change order, first set session value, then call header. Eg.:

    session_start();
    $_SESSION['message'] = "<div class='alert alert-danger'>Please fill all the 
    indicated fields !</div>";
    header('Location: login.php');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看