php字符串验证不起作用

php字符串验证不起作用,php,html,Php,Html,所以我有以下代码: <body> <?php $firstname = $lastname = $phone = $phone = $email = $date = $code = ""; $firstnameerr = $lastnameerr = $phoneerr = $emailerr = $dateerr = $codeerr = ""; $check = 0;

所以我有以下代码:

<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>


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

<?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>


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

<?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>


如果您
var\u dump($final)
您会发现它与表单上的完全不同。您会生成一个随机代码显示在表单上,然后在提交表单时生成另一个。尝试生成它并将其放入会话变量。虽然我感谢您的帮助,但您错了。我用var_dump进行了验证,它给出了完全相同的字符串。您的代码容易受到SQL注入的影响。请在继续之前阅读此内容。请不要卷自己的卫生设备
$code
永远不会等于
$final
,除非有人在访问页面时幸运地获得了与提交页面时相同的准确代码;在任何情况下,访问者都应该立即离开你的网页,去买彩票。我真的认为你应该服用2到3剂PHP汤剂,早上再看一遍你的代码。如果你
var\u dump($final)
你会发现它与你表单上的完全不同。你生成一个随机代码显示在表单上,然后在提交表单时生成另一个。尝试生成它并将其放入会话变量。虽然我感谢您的帮助,但您错了。我用var_dump进行了验证,它给出了完全相同的字符串。您的代码容易受到SQL注入的影响。请在继续之前阅读此内容。请不要卷自己的卫生设备
$code
永远不会等于
$final
,除非有人在访问页面时幸运地获得了与提交页面时相同的准确代码;在任何情况下,访问者都应该立即离开你的网页,去买彩票。我真的认为你应该服用2到3剂PHP汤剂,第二天早上再看一遍你的代码。干得好,尽管你忘了一些重要的东西。您必须在php代码开头通过一个简单的session_start()启动会话。不过,它就像一个符咒,所以谢谢你!:)干得好,不过你忘了一些重要的事情。您必须在php代码开头通过一个简单的session_start()启动会话。不过,它就像一个符咒,所以谢谢你!:)