Php验证脚本错误

时间:2016-06-29 11:00:50

标签: php validation

我有一份注册表格,据我所知昨天工作得很好。我今天早上测试了脚本,我收到以下错误,因为如果没有错误,则错误未定义。如何为每个存在的$ _SESSION ['错误']创建一个foreach,例如如果设置$ _SESSION ['error'] ['firstname']和$ _SESSION ['error'] ['username'],foreach $ _SESSION ['error']将回显现有的$ _SESSION ['error']。

Undefined index: firstname in myurl.com/public_html/register.php on line 29
Undefined index: lastname in myurl.com/public_html/register.php on line 30
Undefined index: password in myurl.com/public_html/register.php on line 33
Undefined index: country in myurl.com/public_html/register.php on line 34

注册表格位于名为register.php的页面上,代码如下......

<?php /* CONNECTION + COUNTRY DROPDOWN */
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
include_once('conn.php');
$querydropdown = "SELECT DISTINCT country FROM countries ORDER by country ASC";
$resultcategories = $mysqli->query($querydropdown);
?>
<html>

<head>


</head>

<body>
<?php getToken(); ?>
<form method="post" action="registerscript.php">
<fieldset>
    <legend>Registration Form</legend>
    <table width="400" border="0" cellpadding="10" cellspacing="10">
        <tr>
            <td></td>
            <td style="font-weight: bold;color:red">

            <?php
                if(isset($_SESSION['error']))
                {
                     echo '<p>'.$_SESSION['error']['firstname'].'</p>';
                     echo '<p>'.$_SESSION['error']['lastname'].'</p>';
                     echo '<p>'.$_SESSION['error']['username'].'</p>';
                     echo '<p>'.$_SESSION['error']['email'].'</p>';
                     echo '<p>'.$_SESSION['error']['password'].'</p>';
                     echo '<p>'.$_SESSION['error']['country'].'</p>';
                     unset($_SESSION['error']);
                }
            ?>

            </td>
        <tr>
            <td style="font-weight: bold"><div align="right"><label for="firstname">First Name</label></div></td>
            <td><input name="firstname" type="text" class="input" size="25" required /></td>
        </tr>           <tr>
            <td style="font-weight: bold"><div align="right"><label for="lastname">Last Name</label></div></td>
            <td><input name="lastname" type="text" class="input" size="25" required /></td>
        </tr>           <tr>
            <td style="font-weight: bold"><div align="right"><label for="username">User Name</label></div></td>
            <td><input name="username" type="text" class="input" size="25" required /></td>
        </tr>           
        <tr>
            <td style="font-weight: bold"><div align="right"><label for="password">Password</label></div></td>
            <td><input name="password" type="password" class="input" size="25" required /></td>
        </tr>
        <tr>
            <td style="font-weight: bold"><div align="right"><label for="confirmpw">Confirm Password</label></div></td>
            <td><input name="confirmpw" type="password" class="input" size="25" required /></td>
        </tr>
        <tr>
            <td style="font-weight: bold"><div align="right"><label for="email">Email</label></div></td>
            <td><input name="email" type="email" class="input" size="25" required /></td>
        </tr>
        <tr>
            <td style="font-weight: bold"><div align="right"><label for="country">Country</label></td>
            <td><select name="country">
                <option selected value="0">Select Country</option>
                    <?php
                            while ($rowCerts = $resultcategories->fetch_assoc()) {
                            echo "<option value=\"{$rowCerts['country']}\">";
                            echo $rowCerts['country'];
                            echo "</option>";
                            }
                    ?>

            </select></td>
        </tr>
        <tr>
            <td height="23"></td>
            <td><div align="right">
            <input type="submit" name="submit" value="Register!" />
            <?php echo getTokenField(); ?>
            </div></td>
        </tr>
    </table>
</fieldset>
 </form>

错误发生在这些线路上......

        <?php
            if(isset($_SESSION['error']))
            {
                 echo '<p>'.$_SESSION['error']['firstname'].'</p>';
                 echo '<p>'.$_SESSION['error']['lastname'].'</p>';
                 echo '<p>'.$_SESSION['error']['password'].'</p>';
                 echo '<p>'.$_SESSION['error']['country'].'</p>';
                 unset($_SESSION['error']);
            }
        ?>

应该从registerscript.php页面检索这些值,其中包含所有验证规则和错误处理。如下图所示...

            <?php
                session_start();
                include_once('conn.php');
                if(isset($_POST['submit']))
                {
                    $firstname = $_POST["firstname"];
                    if(preg_match("/[^a-zA-Z0-9]+/", $firstname))
                        {
                             $_SESSION['error']['firstname'] = "Incorrect characters used in Firstname.<br>";
                        }

                    $lastname = $_POST["lastname"];
                    if(preg_match("/[^a-zA-Z0-9]+/", $lastname))
                        {
                             $_SESSION['error']['lastname'] = "Incorrect characters used in Lastname.<br>";
                        }

                    $username = $_POST["username"];
                    if(preg_match("/[^a-zA-Z0-9]+/", $username))
                        {
                            $_SESSION['error']['username'] = "Incorrect characters used in Username.<br>";
                        }

                    if(strlen($username) < 3)
                        {
                            $_SESSION['error']['username'] = "Username is too short, minimum is 3 characters.<br>";
                        }

                    if(strlen($username) > 15)
                        {
                            $_SESSION['error']['username'] = "Username is too long, maximum is 15 characters.<br>";
                        }
                    if(strlen($_POST['password']) < 6)
                        {
                            $_SESSION['error']['password'] = "Password is too short, minimum is 6 characters.<br>";
                        }
                    if ($_POST['password']!= $_POST['confirmpw'])
                        {
                            $_SESSION['error']['password'] = "Passwords did not match please try again.<br>";
                        }

                    $username = $_POST["username"];
                    $sqlcheckuser = "SELECT * FROM users WHERE username = '$username'";
                    $userresult = mysqli_query($mysqli,$sqlcheckuser) or die(mysqli_error());
                    if (mysqli_num_rows($userresult) > 0)
                        {
                            $_SESSION['error']['username'] = "This Username is already used.<br>";
                        }

                    if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_POST['email']))
                        {

                            $email= $_POST['email'];
                            $sql1 = "SELECT * FROM users WHERE email = '$email'";
                            $result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error());
                            if (mysqli_num_rows($result1) > 0)
                                {
                                    $_SESSION['error']['email'] = "This Email is already used.<br>";
                                }
                        }
                    else
                        {
                            $_SESSION['error']['email'] = "Your email is not valid.<br>";
                        }

                    if(isset($_POST['country']) && $_POST['country'] == '0') 
                        { 
                            $_SESSION['error']['country'] = "Please select a country.<br>"; 
                        } 





                    if(isset($_SESSION['error']))
                    {
                        header("Location: register.php");
                        exit;
                    }
                    else
                    {   
                        $firstname = $_POST["firstname"];
                        $lastname = $_POST["lastname"];
                        $username = $_POST["username"];
                        $email = $_POST['email'];
                        $country = $_POST['country'];
                        $password = $_POST['password'];
                        $confirmcode = md5(uniqid(rand()));

                        $dbpassword = password_hash($password, PASSWORD_BCRYPT);

                        $sql2 = "INSERT INTO users (firstname, lastname, username, email, password, country, confirmcode) VALUES ('$firstname', '$lastname', '$username', '$email', '$dbpassword', '$country', '$confirmcode')";
                        $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error());

                        if($result2)
                        {
                            $to = $email;
                            $subject = "Registration Confirmation - $username";
                            $header = "From: admin \r\n";
                            $message = "Thank you for registering Please click the link to verify and activate your account. ";
                            $message .= "http://www.mywebsite.com/confirm.php?passkey=$confirmcode";

                            $sentmail = mail($to,$subject,$message,$header);

                            if($sentmail)
                            {
                            echo "Your Confirmation link Has Been Sent To Your Email Address.";
                            }
                            else
                            {
                            echo "Cannot send Confirmation link to your e-mail address";
                            }
                        }
                    }
                }
?>

我确信我已定义了所有内容,并且不知道为什么我被告知这些行未定义。请有人告诉我代码被破坏了吗?

0 个答案:

没有答案