PHP注册表单未处理


PHP registration form not processing

我正在注册表格,我有一个问题。无论我在表单上的字段中输入什么,只要我点击提交,表单就会清空,我永远不会被转发到home .php文件。有人知道是什么原因导致这个文件不能被读取吗?谢谢!

        <?php
if (isset($_POST['Submit'])) { // If the form was submitted, process it.
    //function to validate password
    function validatePassword($pass1,$pass2) { 
    if($pass1 === $pass2) 
        {         
          $msg = "Passwords match!"; 
        } 
        else 
        {
          $msg = "Passwords do not match!"; 
        } 
        return $msg;
}
    // Check the username.  
    if (preg_match ("^[[:alnum:]]+$", $_POST['username'])) {
        $a = TRUE;
    } else {
        $a = FALSE;
        $message[] = "Please enter a username that consists only of letters and numbers.";
    }
    // Check to make sure the password is long enough and of the right format. 
    if (preg_match ("^[[:alnum:]]{8,16}$", $_POST['pass1'])) {
            $b = TRUE;
    } else {
            $b = FALSE;
            $message[] = "Please enter a password that consists only of letters and numbers, between 8 and 16 characters long.";    
    }
    // Check to make sure the password matches the confirmed password. 
    if ($_POST['pass1'] == $_POST['pass2']) {
            $c = TRUE;
            $password = crypt ($_POST['pass1']); // Encrypt the password.
    } else {
            $c = FALSE;
            $message[] = "The password you entered did not match the confirmed password.";  
    }
    // Check to make sure they entered a valid email address. 
    if (preg_match ("^([[:alnum:]]|_|'.|-)+@([[:alnum:]]|'.|-)+('.)([a-z]{2,4})$", $_POST['email'])) { 
            $d = TRUE;
    } else {
            $d = FALSE;
            $message[] = "Please enter a valid email address.";
    }
    //  If the data passes all the tests, check to ensure a unique member name, then register them. 
    if ($a AND $b AND $c AND $d) {
        if ($fp = @fopen ("../writeable/users.txt", "r")) { // Open the file for reading.
            while ( !feof($fp) AND !$user_found ) { // Loop through each line checking, each username.
                $read_data = fgetcsv ($fp, 1000, "'t"); // Read the line into an array.
                if ($read_data[0] == $_POST['username']) {
                    $user_found = TRUE;
                }
            }
            fclose ($fp); // Close the file.
            if (!$user_found) { // If the username is OK, register them.
                if ($fp2 = @fopen ("../users.txt", "a")) { // Open the file for writing.
                    $write_data = $_POST['username'] . "'t" . $password . "'t" . $_POST['first_name'] . "'t" . $_POST['last_name'] . "'t" . $_POST['email'] . "'t" . $_POST['birth_month'] . "-" . $_POST['birth_day'] . "-" . $_POST['birth_year'] . "'n";
                    fwrite ($fp2, $write_data); 
                    fclose ($fp2);
                    $message = urlencode ("You have been successfully registered.");
                    header ("Location: homepage.php?message=$message"); // Send them on their way.
                    exit;
                } else {
                    $message[] = "Could not register to the user's file! Please contact the Webmaster for more information.<br />";         
                }
            } else {
                $message[] = "That username is already taken. Please select another.";          
            }
        } else { // If it couldn't open the file, print an error message.
            $message[] = "Could not read the user's file! Please contact the Webmaster for more information.<br />";
        }
    } 

} // End of Submit if.
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Register</title>
</head>
<body>
<?php
// Print out any error messages.
if ($message) {
    echo "<div align='"left'"><font color=red><b>The following problems occurred:</b><br />'n"; 
    foreach ($message as $key => $value) {
        echo "$value <br />'n"; 
    }
    echo "<p></p><b>Be sure to re-enter your passwords!</b></font></div><br />'n";  
}
?>
<form action="" method="POST">
<table border="0" width="90%" cellspacing="2" cellpadding="2" align="center">
    <tr>
        <td align="right">Username</td>
        <td align="left"><input type="text" name="username" size="25" maxsize="16" value="<?=$_POST['username'] ?>"></td>
        <td align="left"><small>Maximum of 16 characters, stick to letters and numbers, no spaces, underscores, hyphens, etc.</small></td>
    </tr>
    <tr>
        <td align="right">Password</td>
        <td align="left"><input type="password" name="pass1" size="25"></td>
        <td align="left"><small>Minimum of 8 characters, maximum of 16, stick to letters and numbers, no spaces, underscores, hyphens, etc.</small></td>
    </tr>
    <tr>
        <td align="right">Confirm Password</td>
        <td align="left"><input type="password" name="pass2" size="25"></td>
        <td align="left"><small>Should be the same as the password.</small></td>
    </tr>
    <tr>
        <td align="right">Email Address</td>
        <td align="left"><input type="text" name="email" size="25" maxsize="60" value="<?=$_POST['email'] ?>"></td>
        <td align="left"><small>Use whichever email address you want to receive notices at.</small></td>
    </tr>
    <tr>
        <td align="center" colspan="3"><input type="submit" name="Submit" value="Register!"> &nbsp; &nbsp; &nbsp; <input type="reset" name="Reset" value="Reset"></td>
    </tr>
</table>
</form>
</body>
</html>

编辑1:我已经替换了$_POSTpreg_match(),我的代码立即做得更多。但是现在,当我提交字段时,我得到:

The following problems occurred:
Please enter a username that consists only of letters and numbers. 
Please enter a password that consists only of letters and numbers, between 8 and 16 characters long. 
Please enter a valid first name. 
Please enter a valid last name. 
Please enter a valid email address. 
Be sure to reenter your passwords and your birth date!

我不明白为什么会发生这种情况,因为我正在正确地将信息输入字段。什么好主意吗?

编辑2:这里是首页。php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Home Page</title>
</head>
<body>
<?php # Script 4.2
if ($message) {
    $message = urldecode($message);
    echo "<div align='"left'"><font color=blue><b>$message</b></font></div><br></br>'n";    
}
?>
</body>
</html>

首先,你必须使用新的资源来学习PHP,因为你似乎从一本旧书或教程中学习。

使用

   $ _POST
不是

   $HTTP_POST_VARS

使用也

   preg_match ()
不是

   eregi ()

这些资源也许可以帮助你达到你的目标

http://www.learn-php.org/

https://www.codecademy.com/tracks/php