PHP字符串验证不工作


php string validation not working

所以我有以下代码:

<body>
          <?php
           $firstname = $lastname = $phone = $phone = $email = $date = $code = "";
           $firstnameerr = $lastnameerr = $phoneerr = $emailerr = $dateerr = $codeerr = "";
           $check = 0;
           $str = "abcdefghijklmnopqrstuvwxyz";
           $rand1 = $str[rand(0, strlen($str) - 1)];
           $rand2 = $str[rand(0, strlen($str) - 1)];
           $rand3 = $str[rand(0, strlen($str) - 1)];
           $rand4 = $str[rand(0, strlen($str) - 1)];
           $rand5 = $str[rand(0, strlen($str) - 1)];
           $final = $rand1 . $rand2 . $rand3 . $rand4 . $rand5;
           if ($_SERVER["REQUEST_METHOD"] == "POST"){  
               if (empty($_POST["ffirstname"])){
                   $firstnameerr = "First Name is empty!";
                   $check = 1;
               } else {
                    $firstname = testInput($_POST['ffirstname']);
                    $check = 0;
                    if (!preg_match("/^[a-zA-Z]*$/",$firstname)){
                        $firstnameerr = "This is not a valid name!";
                        $check = 1;
                    }
               }
               if (empty($_POST["flastname"])){
                   $lastnameerr = "Last Name is empty!";
                   $check = 1;
               } else {
                    $lastname = testInput($_POST['flastname']);
                    $cheek = 0;
                    if (!preg_match("/^[a-zA-Z ]*$/",$lastname)){
                        $lastnameerr = "This is not a valid name";
                        $check = 1;
                    }
               }
               if (empty($_POST["fphone"])){
                   $phoneerr = "Phone field is empty!";
                   $check = 1;
               }else {
                    $phone = testInput($_POST['fphone']);
                    if(!is_numeric($phone)){
                        $phoneerr = "Phone number is not a number";
                        $check = 1;
                    }
               }
               if (empty($_POST["femail"])){
                   $emailerr = "E-mail field is empty!";
               } else {
                   $email = testInput($_POST['femail']);
                   if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                       $emailerr = "E-mail is not valid";
                       $check = 1;
                   }
               }
               if (empty($_POST["fdate"])){
                   $dateerr = "No date selected!";
                   $check = 1;
               } else {
                   $date = testInput($_POST['fdate']);
               }
               if (empty($_POST["fcode"])){
                   $codeerr = "There is no code!";
                   $check = 1;
               } else {
                   $code = $_POST["fcode"];
                   if ($code !== $final){
                       $codeerr = "The code is wrong";
                       $check = 1;
                   }
               }
               if ($check == 0) {     
                    $host = "localhost";
                    $user = "root";
                    $pass = "";
                    $db = "myfirstdb";
                    $connect = new mysqli($host,$user,$pass,$db);
                    if ($connect->connect_error){ 
                        die("Connection failed: " . $connect->connect_error);
                    } else {
                        echo "Connected successfully!";
                    }
                    $sql = "INSERT INTO table1 (firstname , lastname , phone , email , date) VALUES ('$firstname', '$lastname', '$phone', '$email', '$date')";
                    if ($connect->query($sql) === TRUE) {
                        echo "New record created successfully";
                    } else {
                        echo "Error: " . $sql . "<br>" . $connect->error;
                    }
                    $connect->close(); 
                }
            }
            function testInput($data){
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                return $data;
            }
    ?>
        <div id="header">
            <img src="http://stupidname.org/files/gfx/design/random%20logos/RandomLogo1.png" alt="logo" height="250px" width="250px">
            <div id="top"><h1 id="first">Welcome to my website</h1></div>
        </div>
        <div id="section">
            <div id="nav">
                <ul>
                    <li><a href="LINK1" id="first">Home</a></li>
                    <li><a href="LINK2">About</a></li>
                    <li><a href="LINK3">Project</a></li>
                    <li><a href="LINK4">Contact</a></li>
                </ul>
            </div>
            <div id="article">
                <h3 style="text-align: center"><b>Please confirm the form below:</b></h3>
                <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                    <p class="namer">First Name</p><br>
                    <input type="text" name="ffirstname" id="ffirstnameid"><span class="error"><?php echo $firstnameerr; ?></span><br>
                    <p class="namer">Last Name</p><br>
                    <input type="text" name="flastname" id="flastnameid"><span class="error"><?php echo $lastnameerr; ?></span><br>
                    <p class="namer">Phone Number</p><br>
                    <input type="text" name="fphone" id="fphoneid"><span class="error"><?php echo $phoneerr; ?></span><br>
                    <p class="namer">E-mail</p><br>
                    <input type="text" name="femail" id="femailid"><span class="error"><?php echo $emailerr; ?></span><br>
                    <p class="namer">Date</p><br>
                    <input type="text" name="fdate" id="fdateid"><span class="error"><?php echo $dateerr; ?></span><br>
                    <p class="namer">Enter the Captcha code!</p><br>
                    <h1><?php echo $final?></h1><br>
                    <input type="text" name="fcode" id="fcodeid"><span class="error"><?php echo $codeerr; ?></span><br>
                    <input type="submit" name="fsubmit" value="Submit">
                </form>
            </div>
        </div>

我的问题是与代码a.k.a如果使用$code和$final检查它是否是一个人或不是。现在,每当我在$final变量中写入完全相同的东西时,程序认为它不一样,所以我得到$codeerr。有人能帮我修一下吗?

好的,我对你的代码做了一些修改,我想现在应该可以工作了。

<?php
session_start();
?>
<body>
      <?php
       function generateCode() {
           $str = "abcdefghijklmnopqrstuvwxyz";
           $rand1 = $str[rand(0, strlen($str) - 1)];
           $rand2 = $str[rand(0, strlen($str) - 1)];
           $rand3 = $str[rand(0, strlen($str) - 1)];
           $rand4 = $str[rand(0, strlen($str) - 1)];
           $rand5 = $str[rand(0, strlen($str) - 1)];
           return $rand1 . $rand2 . $rand3 . $rand4 . $rand5;
       }
       $firstname = $lastname = $phone = $phone = $email = $date = $code = "";
       $firstnameerr = $lastnameerr = $phoneerr = $emailerr = $dateerr = $codeerr = "";
       $check = 0;
       if(!isset($_SESSION['final'])) {
           $_SESSION['final'] = generateCode();
       }
       if ($_SERVER["REQUEST_METHOD"] == "POST"){  
           if (empty($_POST["ffirstname"])){
               $firstnameerr = "First Name is empty!";
               $check = 1;
           } else {
                $firstname = testInput($_POST['ffirstname']);
                $check = 0;
                if (!preg_match("/^[a-zA-Z]*$/",$firstname)){
                    $firstnameerr = "This is not a valid name!";
                    $check = 1;
                }
           }
           if (empty($_POST["flastname"])){
               $lastnameerr = "Last Name is empty!";
               $check = 1;
           } else {
                $lastname = testInput($_POST['flastname']);
                $cheek = 0;
                if (!preg_match("/^[a-zA-Z ]*$/",$lastname)){
                    $lastnameerr = "This is not a valid name";
                    $check = 1;
                }
           }
           if (empty($_POST["fphone"])){
               $phoneerr = "Phone field is empty!";
               $check = 1;
           }else {
                $phone = testInput($_POST['fphone']);
                if(!is_numeric($phone)){
                    $phoneerr = "Phone number is not a number";
                    $check = 1;
                }
           }
           if (empty($_POST["femail"])){
               $emailerr = "E-mail field is empty!";
           } else {
               $email = testInput($_POST['femail']);
               if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                   $emailerr = "E-mail is not valid";
                   $check = 1;
               }
           }
           if (empty($_POST["fdate"])){
               $dateerr = "No date selected!";
               $check = 1;
           } else {
               $date = testInput($_POST['fdate']);
           }
           if (empty($_POST["fcode"])){
               $codeerr = "There is no code!";
               $check = 1;
           } else {
               $code = $_POST["fcode"];
               if ($code !== $_SESSION['final']){
                   $codeerr = "The code is wrong";
                   $check = 1;
               }
           }
           if ($check == 0) {     
                $host = "localhost";
                $user = "root";
                $pass = "";
                $db = "myfirstdb";
                $connect = new mysqli($host,$user,$pass,$db);
                if ($connect->connect_error){ 
                    die("Connection failed: " . $connect->connect_error);
                } else {
                    echo "Connected successfully!";
                }
                $sql = "INSERT INTO table1 (firstname , lastname , phone , email , date) VALUES ('$firstname', '$lastname', '$phone', '$email', '$date')";
                if ($connect->query($sql) === TRUE) {
                    echo "New record created successfully";
                } else {
                    echo "Error: " . $sql . "<br>" . $connect->error;
                }
                $connect->close(); 
            }
        }
        if($check == 1) {
            $_SESSION['final'] = generateCode();
        }
        function testInput($data){
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }
?>
    <div id="header">
        <img src="http://stupidname.org/files/gfx/design/random%20logos/RandomLogo1.png" alt="logo" height="250px" width="250px">
        <div id="top"><h1 id="first">Welcome to my website</h1></div>
    </div>
    <div id="section">
        <div id="nav">
            <ul>
                <li><a href="LINK1" id="first">Home</a></li>
                <li><a href="LINK2">About</a></li>
                <li><a href="LINK3">Project</a></li>
                <li><a href="LINK4">Contact</a></li>
            </ul>
        </div>
        <div id="article">
            <h3 style="text-align: center"><b>Please confirm the form below:</b></h3>
            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                <p class="namer">First Name</p><br>
                <input type="text" name="ffirstname" id="ffirstnameid"><span class="error"><?php echo $firstnameerr; ?></span><br>
                <p class="namer">Last Name</p><br>
                <input type="text" name="flastname" id="flastnameid"><span class="error"><?php echo $lastnameerr; ?></span><br>
                <p class="namer">Phone Number</p><br>
                <input type="text" name="fphone" id="fphoneid"><span class="error"><?php echo $phoneerr; ?></span><br>
                <p class="namer">E-mail</p><br>
                <input type="text" name="femail" id="femailid"><span class="error"><?php echo $emailerr; ?></span><br>
                <p class="namer">Date</p><br>
                <input type="text" name="fdate" id="fdateid"><span class="error"><?php echo $dateerr; ?></span><br>
                <p class="namer">Enter the Captcha code!</p><br>
                <h1><?php echo $_SESSION['final']?></h1><br>
                <input type="text" name="fcode" id="fcodeid"><span class="error"><?php echo $codeerr; ?></span><br>
                <input type="submit" name="fsubmit" value="Submit">
            </form>
        </div>
    </div>

必须将$final代码保存在$_SESSION中,因为在提交表单后,生成$final的代码将被执行,而$final将获得与提交前渲染代码不同的新值。