我正在处理注册表格并遇到问题。无论我输入到表单上的字段,每当我点击提交时,表单只是清除,我永远不会转发到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>
答案 0 :(得分:1)
首先,您必须使用新资源来学习PHP,因为它似乎是从旧书或教程中学习的。
使用
$ _POST
INSTEAD
$HTTP_POST_VARS
也使用
preg_match ()
INSTEAD
eregi ()
这些资源可以帮助您实现目标