[英]PHP registration form not processing
我正在處理注冊表,但遇到了問題。 無論我在表單上的字段中輸入什么,每當我點擊提交時,表單都會被清除,我永遠不會被轉發到 homepage.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!"> <input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</form>
</body>
</html>
編輯 1:我已經替換了$_POST
和preg_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:這是 homepage.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 ()
這些資源或許可以幫助您實現目標
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.