PHP多步注册过程

我正在研究一个包含3个步骤的注册系统.

Step 1 – user enters a username, system searches the database for the
username. IF the username is found, it checks the account status (ie:
no password created, complete but not verified, registered and
verified).

如果未找到用户,则将用户转至步骤2.

如果status =未创建密码,则将用户转至步骤3.

如果status = complete但未验证/注册和验证,则显示错误消息.

Step 2 – user enters personal details.

该页面存储用户输入

Step 3 – user creates a password, the system connects to the database and INSERTs user info to the user table. A success message is
displayed.

通过在提交上一个表单时显示新表单,我设法找出并完成前两个步骤的编码.

问题:但是,我刚刚意识到我无法从上一个表单中检索数据(即:在步骤3中,我无法从步骤1中检索用户名).我试过使用’header(‘location:?user = $uname’);’然而,这种方法很有效,因为当我提交新表单时,URL会被重置,我又会丢失用户名.如何使用ONLY PHP创建正确的多步骤表单,如何存储输入值,以便在最后一步使用它们.以下是我的代码:

<?php
include 'includes/session_info.php';
if(isset($_SESSION['user_id'])){
    header('Location: index.php');
}
$errors = array();
if(empty($_POST['user_info']) === false){
    require ('core/dbcon.php');
    $usr_email = mysqli_real_escape_string($con, $_POST['email']);
    $usr_joined = mysqli_real_escape_string($con, $_POST['joined']);
    $usr_recruited = mysqli_real_escape_string($con, $_POST['recruited']);
    if($usr_email){
        //direct user to password form
    }else{
        $errors[] = 'Please complete all fields marked with a Red Asterisk.';
    }
    $form2 = $usr_email.'<br>'.$usr_joined.'<br>'.$usr_recruited;
}
if(empty($_POST['username_chck']) === false){
    require ('core/dbcon.php');
    $username = mysqli_real_escape_string($con, $_POST['uname']);
    $rpt_uname = mysqli_real_escape_string($con, $_POST['r_uname']);
    if($username && $rpt_uname){
        if($username == $rpt_uname){
            $query = mysqli_query($con, "SELECT status FROM users WHERE username = '$username'") or die(mysqli_error($con));
            // Display registration form if Username is not found.
            if(mysqli_num_rows($query) == 0){
                $form1;
            }
            // Actions performed If username entered already exists in the database.
            elseif(mysqli_num_rows($query) == 1){
                $status = mysqli_fetch_assoc($query);
                if($status['status'] == 0){
                    $errors[] = '<b>'.$username.'</b> is already registered and awaiting to be verified by our admins. Feel free to contact an Admin via the website or in-game to get verified.';
                }elseif($status['status'] == 1){
                    //header("Location:?create_pwd&user=$username");
                }elseif($status['status'] > 1){
                    $errors[] = '<b>'.$username.'</b> is already registered and verified by our Admins. Please log in to access you account.
                    If you have forgotten your password you can rest your password <a class="navbar-link error_link" id="intext-link" href="login.php?fp"><b>here</b></a>.';
                }
            }elseif(mysqli_num_rows($query) > 1){
                $errors[] = 'An error has occurred. Looks like a there is more than one member with that username. Please contact the Administrator for assistance.';
            }
        }else{
            $errors[] = 'Please ensure that the username entered in both fields match.';

        }
    }else{
        $errors[] = 'Please complete all required fields.';
    }
}

?>

<html>
<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <?php   
            if(empty($_POST['username_chck']) === false){
                if(empty ($errors) === false){
        ?>          
                    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                        <div class="form-group">
                            <label for="Uname"><span class="glyphicon glyphicon-asterisk required" aria-hidden="true"></span> Username: </label><br>
                            <input type="text" name="uname" class="form-control" placeholder="Please enter your Runescape username." id="Uname" required>
                        </div>
                        <div class="form-group">
                            <label for="repeat_Uname"><span class="glyphicon glyphicon-asterisk required" aria-hidden="true"></span> Repeat Username: </label><br>
                            <input type="text" name="r_uname" class="form-control" id="repeat_Uname"  placeholder="Please re-enter your Runescape username." required>
                        </div>
                        <input type="submit" name="username_chck" class="btn btn-default" value ="Next">
                    </form>
        <?php
                }else{ echo $reg_uname;
        ?>
                    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                        <div class="form-group">
                            <label for="Email"><span class="glyphicon glyphicon-asterisk required" aria-hidden="true"></span> Email: </label>
                            <input type="email" name="email" class="form-control" id="Email"  <?php if (isset($_POST['email'])=== true){echo 'value="', strip_tags($_POST['email']),'"';}?>>
                        </div>
                        <div class="form-group">
                            <label for="Joined">Date Joined: </label><br>
                            <small class="notice">If you do not remember the exact date please select the first day of the month and year you joined (eg: 01/02/2001).</small><br>
                            <input type="date" name="joined" class="form-control" id="Joined" <?php if (isset($_POST['joined'])=== true){echo 'value="', strip_tags($_POST['joined']),'"';}?>>
                        </div>
                        <div class="form-group">
                            <label for="recruited">Recruited by: </label>
                            <select name="recruited" class="form-control" id="recruited">
                                <option value="" selected disabled>Select a Member</option>
                                <?php
                                    require ('core/dbcon.php');
                                    $usr_qry = mysqli_query($con, "SELECT user_id, username FROM users")or die(mysqli_error($con));
                                    while($usr = mysqli_fetch_array($usr_qry)){
                                        echo '<option value="'.$usr['user_id'].'">'.$usr['username'].'</option>';
                                    }
                                ?>
                            </select>
                        </div>
                        <input type="submit" name="user_info" class="btn btn-default" value ="Next">
                    </form>
        <?php
                }
            }elseif(empty($_POST['user_info']) === false){
                if(empty ($errors) === false){
        ?>
                    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                        <div class="form-group">
                            <label for="Email"><span class="glyphicon glyphicon-asterisk required" aria-hidden="true"></span> Email: </label>
                            <input type="email" name="email" class="form-control" id="Email"  <?php if (isset($_POST['email'])=== true){echo 'value="', strip_tags($_POST['email']),'"';}?>>
                        </div>
                        <div class="form-group">
                            <label for="Joined">Date Joined: </label><br>
                            <small class="notice">If you do not remember the exact date please select the first day of the month and year you joined (eg: 01/02/2001).</small><br>
                            <input type="date" name="joined" class="form-control" id="Joined" <?php if (isset($_POST['joined'])=== true){echo 'value="', strip_tags($_POST['joined']),'"';}?>>
                        </div>
                        <div class="form-group">
                            <label for="recruited">Recruited by: </label>
                            <select name="recruited" class="form-control" id="recruited">
                                <option value="" selected disabled>Select a Member</option>
                                <?php
                                    require ('core/dbcon.php');
                                    $usr_qry = mysqli_query($con, "SELECT user_id, username FROM users")or die(mysqli_error($con));
                                    while($usr = mysqli_fetch_array($usr_qry)){
                                        echo '<option value="'.$usr['user_id'].'">'.$usr['username'].'</option>';
                                    }
                                ?>
                            </select>
                        </div>
                        <input type="submit" name="user_info" class="btn btn-default" value ="Next">
                    </form>
        <?php
                }else
                    echo $reg_uname.'<br>'. $reg_email.'<br>'.$reg_joined.'<br>'.$reg_recruited.'<br>';
            }else{
        ?>
                <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                    <div class="form-group">
                        <label for="Uname"><span class="glyphicon glyphicon-asterisk required" aria-hidden="true"></span> Username: </label><br>
                        <input type="text" name="uname" class="form-control" placeholder="Please enter your Runescape username." id="Uname" required>
                    </div>
                    <div class="form-group">
                        <label for="repeat_Uname"><span class="glyphicon glyphicon-asterisk required" aria-hidden="true"></span> Repeat Username: </label><br>
                        <input type="text" name="r_uname" class="form-control" id="repeat_Uname"  placeholder="Please re-enter your Runescape username." required>
                    </div>
                    <input type="submit" name="username_chck" class="btn btn-default" value ="Next">
                </form>
        <?php
            }
        ?>
    </div>
</div>
</html>

PS.我已经研究过创建一个会在用户导航离开页面Destroy PHP session on page leaving时被销毁的会话.但是我发现它不是非常用户友好,因为如果用户打开了多个标签,它就无法正常工作.我知道我需要实现一个javascript函数才能使它正常工作.我不知道如何使用javascript进行编码,非常感谢您在制定更好的多步注册流程方面提供的帮助.

最佳答案 如上所述,存储会话变量中每个步骤的POST数据.

// Step 1 submit
$_SESSION['steps'][1] = $_POST;

// Step 2 submit
$_SESSION['steps'][2] = $_POST;

// Step 3 submit
$_SESSION['steps'][3] = $_POST;

然后,您可以在会话中使用类似currentStep的内容来确定它们的最后位置.

$currentStep = $_POST['step'];

并与您需要的数据进行比较,或者直接从数组中使用它.

点赞