Php 在注册表中显示消息时出现问题

Php 在注册表中显示消息时出现问题,php,Php,我对PHP相当陌生,我正在尝试建立一个登录系统。我正在尝试输出错误或成功消息以显示在与表单相同的页面中。我尝试使用“echo”进行测试,它可以工作,但是它会在新选项卡中显示消息。因此,我尝试将消息转换为一个变量,因此在每种情况下,该变量都会使用新消息进行更新。然后在register.php页面上,我输入一个php代码,检查消息是否为空,然后回显变量。问题是我被重定向到register.php页面,但什么也没有显示。这是我的两个文件的代码,我应该提到,注册过程确实有效,它检查电子邮件和用户名,如果

我对PHP相当陌生,我正在尝试建立一个登录系统。我正在尝试输出错误或成功消息以显示在与表单相同的页面中。我尝试使用“echo”进行测试,它可以工作,但是它会在新选项卡中显示消息。因此,我尝试将消息转换为一个变量,因此在每种情况下,该变量都会使用新消息进行更新。然后在register.php页面上,我输入一个php代码,检查消息是否为空,然后回显变量。问题是我被重定向到register.php页面,但什么也没有显示。这是我的两个文件的代码,我应该提到,注册过程确实有效,它检查电子邮件和用户名,如果没有重复的,但是它不会输出任何消息

register.php:

<?php
    include 'header.php';
    include 'nav.php';
    include 'handlers/registerhandler.php';
    require 'handlers/dbhandler.php';
?>

    <section>
        <div class="wrapper">
            <p>If you already have an account, click <a href="index.php">here</a> to login.</p>
        </div>

        <div class="wrapper">
            <?php
                if (!empty($msg)) {
                    echo $msg;
                }
            ?>
            <form action="register.php" method="POST">
                <ul>
                    <li><input type="text" name="firstname" placeholder="Enter your first name ..." required></li>
                    <li><input type="text" name="lastname" placeholder="Enter your last name ..." required></li>
                    <li><input type="email" name="email" placeholder="Enter your email adress ..." required></li>
                    <li><input type="text" name="username" placeholder="Enter your username ..." required></li>
                    <li><input type="password" name="password" placeholder="Enter your password ..." required></li>
                    <li><input type="password" name="confirmpassword" placeholder="Confirm your password ..." required></li>
                    <li><button type="submit" name="submit" value=1>Register</button></li>
                </ul>
            </form>
        </div>
    </section>

<?php
    include 'footer.php';
?>

如果您已经有帐户,请单击以登录

  • 登记册
以及registerhandler.php:

<?php

session_start();
require 'dbhandler.php';

if (isset($_POST['submit'])) {

    $msg = '';

    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $confirmpassword = $_POST['confirmpassword'];

    $usernameExcistCheck = "SELECT * FROM user WHERE username='$username'";
    $usernameCheckResult = mysqli_query($connect, $usernameExcistCheck);
    $emailExcistCheck = "SELECT * FROM user WHERE email='$email'";
    $emailCheckResult = mysqli_query($connect, $emailExcistCheck);

    while ($usernameRow = mysqli_fetch_assoc($usernameCheckResult)) {
        $db_username = $usernameRow["username"];
    }
    while ($emailRow = mysqli_fetch_assoc($emailCheckResult)) {
        $db_email = $emailRow["email"];
    }


    if ($db_username == $username) {
        $msg = "Username already excists.";
    } else {
        if ($db_email == $email) {
            $msg = "E-mail is already registered";
        } else {
            if($password != $confirmpassword) {
            die("Passwords do not match! Please try again.");       
            } else {
                $confirmcode = rand();
                $sql = "INSERT INTO user (firstname, lastname, email, username, password, confirmed, confirmcode)
                VALUES ('$firstname', '$lastname', '$email', '$username', '$password', '0', '$confirmcode')";
                $result = mysqli_query($connect, $sql);

                if ($result) {
                        $to = $email;
                        $subject = "Activate your DuChambre.nl account!";
                        $message = "<html>
                        <head>
                        <title>Activate your DuChambre.nl account</title>
                        </head>
                        <body>
                        <p>Dear $firstname $lastname,<br><br>Your account needs to be activated in order to use it. Please click <a href='http://www.duchambre.nl/duchambre_loginsystem/verifyemail.php?code=$confirmcode&email=$email'>here</a> to activate your account. You could also copy and paste the link below.<br><br>http://www.duchambre.nl/duchambre_loginsystem/verifyemail.php?code=$confirmcode&email=$email<br><br>Please do not reply to this email. Enjoy your stay!<br><br>Cheers,<br>Bas
                        </p>
                        </body>
                        </html>";

                        $headers = "MIME-Version: 1.0" . "\r\n";
                        $headers .= "Content-type: text/html; charset=iso-8859-1" ."\r\n";
                        $headers .= 'From: <activation@duchambre.nl>' . "\r\n";

                        $mail = mail($to, $subject, $message, $headers);

                        if ($mail) {

                            $msg = "Your account has been created, check your email to activate your account.";

                            //header("Location: register.php");  

                        } else {
                            $msg = "Something went wrong, please try again.";
                        }
                }
            }
        }    
    }
}
?>


我想我遗漏了什么,但我已经花了3个小时来修复这个问题。提前感谢您的帮助。

尝试在
header.php
之前包含
registerhandler.php
(在页面顶部),您将
dbhandler.php
包含在
register.php
registerhandler.php
中。只包括一次!在header.php工作之前包括registerhandler.php!我不知道有那么简单。我也不知道它的重要性,在什么顺序,你包括(除了明显的导航和标题,因为否则你的导航将高于你的标题)。谢谢<代码>会话应首先启动以访问会话变量。更多信息请访问