if语句- exit()函数在php脚本中的位置


if statement - Where does the exit() function go in my php script?

我是php的新手,我创建了一个用户注册表单。但是每当我提交表单时,页面的一部分无法加载。我看到了"成功注册"消息,但没有看到右侧栏、页脚或包装器div。在搜索了一整天的互联网后,我得出结论,这是exit()函数。但是,如果我在退出函数中离开并提交表单,我的所有页面都会加载,但是原始注册表单仍然显示在"成功注册"消息的下面。有人能帮帮我吗?

            <?php
            require_once('_includes/connectvars.php');
            if (isset($_POST['submitted'])) {
            $firstname = mysql_real_escape_string(trim($_POST['firstname']));
            $lastname = mysql_real_escape_string(trim($_POST['lastname']));
            $email = mysql_real_escape_string(trim($_POST['email']));
            $birthdate = mysql_real_escape_string(trim($_POST['birthdate']));
            $zipcode = mysql_real_escape_string(trim($_POST['zipcode']));
            $gender = $_POST['gender'];
            $username = mysql_real_escape_string(trim($_POST['username']));
            $password1 = mysql_real_escape_string(trim($_POST['password1']));
            $password2 = mysql_real_escape_string(trim($_POST['password2']));
            $terms = $_POST['terms'];
            $newsletter = $_POST['newsletter'];
            $activationkey = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
            $captchchk = 1;
            require_once('_includes/recaptchalib.php');
            $privatekey = "some#";
            $resp = recaptcha_check_answer ($privatekey,
            $_SERVER["REMOTE_ADDR"],
            $_POST["recaptcha_challenge_field"],
            $_POST["recaptcha_response_field"]);
            if (preg_match ('%^[A-Za-z'.'' '-]{2,15}$%', stripslashes(trim($_POST['firstname'])))) {
            $firstname = escape_data($_POST['firstname']);
            } else {
                $firstname = FALSE;
                echo '<p class="error">Please enter a valid first name!</p>';
                }
            if (preg_match ('%^[A-Za-z'.'' '-]{2,30}$%', stripslashes(trim($_POST['lastname'])))) {
                $lastname = escape_data($_POST['lastname']);
                } else {
                    $lastname = FALSE;
                    echo '<p class="error">Please enter a valid last name!</p>';
                    }
            if (preg_match('%^(0?[1-9]|1[012])[/](0?[1-9]|[12][0-9]|3[01])[/](19|20)?[0-9]{2}$%', stripslashes(trim($_POST['birthdate'])))) {
                $birthdate = escape_data($_POST['birthdate']);
                } else {
                    $birthdate = FALSE;
                    echo '<p class="error">Please enter a valid date of birth in the format of MM/DD/YYYY</p>'; 
                    }
            if (preg_match ('%^[0-9]{5}$%', stripslashes(trim($_POST['zipcode'])))) {
                $zipcode = escape_data($_POST['zipcode']);
                } else {
                    $zipcode = FALSE;
                    echo '<p class="error">Please enter a valid 5 digit zip code!</p>';
                    }
            if ($gender !== 'M') {
                if ($gender !== 'F') {
                    echo '<p class="error">Please select your gender!</p>';
                    }
                }
            if (preg_match ('%^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*('S{4,75})$%', stripslashes(trim($_POST['username'])))) { 
                $username = escape_data($_POST['username']);
                } else {
                    $username = FALSE;
                    echo '<p class="error">Please enter a valid username!</p>';
                    }
            if (preg_match ('%^[A-Za-z0-9]{6,15}$%', stripslashes(trim($_POST['password1'])))) {
                $password1 = escape_data($_POST['password1']);
                if (($_POST['password1'] == $_POST['password2']) && ($_POST['password1'] != $_POST['username'])) {
                     $password1 = escape_data($_POST['password1']);
                     } elseif ($_POST['password1'] == $_POST['username']) {
                         $password1 = FALSE;
                         echo '<p class="error">Your password cannot be the same as the username!</p>';
                         } else {
                             $password1 = FALSE;
                             echo '<p class="error">Your password did not match the confirmed password!</p>';
                             }
                             } else {
                                 $password1 = FALSE;
                                 echo '<p class="error">Please enter a valid password!</p>';
                                 }
            if ($terms !== 'Y') {
                echo '<p class="error">You must agree to the terms of use!</p>';
                }
            if (!$resp->is_valid) {
                echo '<p class="error">The CAPTCHA Code wasn''t entered correctly!</p>';
                $captchchk = 0;
                }
                if ($firstname && $lastname && $email && $birthdate && $zipcode && $gender && $username && $password1 &&
                  $terms && $captchchk) {
                      $query = "SELECT * FROM members WHERE username = '$username'";
                      $result = mysql_query($query) or trigger_error(mysql_error().$query);
                      if(mysql_num_rows($result) == 1) {
                          echo '<br><br><p class="error">An account already exists for this username. Please select a different
                          username.</p>';
                          $username = "";
                          } else {
                              if(mysql_num_rows($result) == 0) {
                                  $query = "INSERT INTO members (first_name, last_name, email, dob, zip_code, gender, username,     password, join_date, newsletter, active, terms, status) VALUES ('$firstname', '$lastname', '$email', STR_TO_DATE('$birthdate', '%m/%d/%Y'), '$zipcode', '$gender', '$username', SHA('$password1'), NOW(), '$newsletter', '$activationkey', '$terms', 'verify')";
                                  $result = mysql_query($query) or trigger_error(mysql_error().$query);
                                  if (mysql_affected_rows() == 1) {
                                      $to = $_POST['email'];
                                      $subject = "Registration";
                                      $message = "Welcome to Mysite.com!'n'nYou, or someone using your email address, has completed registration at Mysite.com. You can complete registration by clicking the following link:'n'nhttp://www.mysite.com/verify.php?$activationkey'n'nIf this is an error, ignore this email and you will be removed from our mailing list.'n'nRegards,'n'nThe Mysite.com Team";
                                      $headers = 'From: noreply@ Mysite.com' . "'r'n" .
                                      'Reply-To: noreply@ Mysite.com' . "'r'n" .
                                      'X-Mailer: PHP/' . phpversion();
                                      mail($to, $subject, $message, $headers);
                                      echo '<br /><br /><p>Thank you for registering! A confirmation email has been sent to your email. Please click on the link in that email in order to activate your account.</p>';
                                      } else {
                                          $queryString = $_SERVER['QUERY_STRING'];
                                          $query = "SELECT * FROM members";
                                          $result = mysql_query($query) or trigger_error(mysql_error().$query);
                                          while($row = mysql_fetch_array($result)){
                                              if ($queryString == $row["active"]){
                                                  echo "Congratulations!" . $row["username"] . " is now a member of Mysite.com!";
                                                   $query="UPDATE members SET active = '', status='activated' WHERE (user_id =
                                                   $row[user_id])";
                                                   if (!mysql_query($query)) {
                                                       die('Error: ' . mysql_error());
                                                       }
                                              }
                                          }
                                      }
                              }
                          }
                  }
                  exit();
                  mysql_close();
            }

您不需要exit(),只需要if ... else块:

if ((/* form submitted */) && (/* everything is OK */))
{
    // do stuff
}
else
{
    // show form
}

有两种解决方案。

  1. 您可以在提交时将表单重定向到另一个包含表单处理和感谢信息的逻辑页面。
  2. 您可以将初始表单和感谢消息/处理包装在有条件的if块中,以确定要显示哪些内容。