验证后重定向到成功页面

时间:2016-11-05 09:11:10

标签: php

我想在验证成功时使用php标头重定向。然而,它似乎对我不利。那我该如何实现呢?条件是验证所有验证后,它只会重定向。

   <?php
         // define variables and set to empty values
         $nameErr = $lastnameErr = $emailErr = $passwordErr = $confirmpasswordErr = $checkboxErr=  "";
         $name = $lastname = $email = $password = $confirmpassword = $checkbox = "";

         if ($_SERVER["REQUEST_METHOD"] == "POST") {

            if (empty($_POST["firstname"])) {
               $nameErr = "First Name is required";
            }else {
               $name = test_input($_POST["firstname"]);
            }

                 if (empty($_POST["lastname"])) {
               $lastnameErr = "Last Name is required";
            }else {
               $name = test_input($_POST["lastname"]);
            }

            if (empty($_POST["email"])) {
               $emailErr = "Email is required";
            }else {
               $email = test_input($_POST["email"]);

               // check if e-mail address is well-formed
               if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                  $emailErr = "Invalid email format"; 
               }
            }


            if(!empty($_POST["password"]) && ($_POST["password"] == $_POST["confirmpassword"])) {
                $password = test_input($_POST["password"]);
                $confirmpassword = test_input($_POST["confirmpassword"]);
                if (strlen($_POST["password"]) <= '8') {
                    $passwordErr = "Your Password Must Contain At Least 8 Characters!";
                }
                elseif(!preg_match("#[0-9]+#",$password)) {
                    $passwordErr = "Your Password Must Contain At Least 1 Number!";
                }
                elseif(!preg_match("#[A-Z]+#",$password)) {
                    $passwordErr = "Your Password Must Contain At Least 1 Capital Letter!";
                }
                elseif(!preg_match("#[a-z]+#",$password)) {
                    $passwordErr = "Your Password Must Contain At Least 1 Lowercase Letter!";
                }
            }
            elseif(empty($_POST["password"])) {
                $passwordErr = "Password not filled at all";
            }

            elseif(!empty($_POST["password"])) {
                $confirmpasswordErr = "Password do not match";
            }

        if(!isset($_POST['checkbox'])){ 
        $checkboxErr = "Please check the checkbox";
        }
        else {
               $checkbox = test_input($_POST["checkbox"]);

            }









         }



         function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
         }
      ?>

  header('Location: http://www.example.com/');

2 个答案:

答案 0 :(得分:0)

如果任何条件失败,请设置$ error = 1,并在底部检查是否($ error!= 1)然后重定向 如果标题不起作用,你也可以使用javascript重定向

答案 1 :(得分:-1)

  1. 查看结束&#34;?&gt;&#34; -Tab。 header会生成一个html-header,但是是一个php函数,应该在?php?&gt;里面?支架

  2. 考虑使用html5输入验证 - 保存一些代码和服务器往返,让浏览器进行验证

  3. 省略结束&#34;?&gt;&#34;共。它不是必需的,并且在&#34;?&gt;&#34;

  4. 之后有内容 - 甚至是空白 - 时会导致很难看到错误
  5. 考虑使用带有适当参数的filter_input函数来访问$ _POST并设置变量。