PHP注册表未处理
我正在写一份登记表,有个问题。无论我在表单的字段中输入什么,只要我点击submit,表单就会被清除,并且我永远不会被转发到homepage.php文件。是否有人看到导致此文件无法读取的原因?谢谢PHP注册表未处理,php,Php,我正在写一份登记表,有个问题。无论我在表单的字段中输入什么,只要我点击submit,表单就会被清除,并且我永远不会被转发到homepage.php文件。是否有人看到导致此文件无法读取的原因?谢谢 <?php if (isset($_POST['Submit'])) { // If the form was submitted, process it. //function to validate password function validatePas
<?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>
我不明白为什么会发生这种情况,因为我在字段中正确输入了信息。有什么想法吗
编辑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,因为您似乎从旧书或教程中学习
使用
而不是
$HTTP_POST_VARS
eregi ()
也使用
preg_match ()
而不是
$HTTP_POST_VARS
eregi ()
这些资源可能会帮助你实现目标
您的代码已过时,并且已使用多年。从编写未被删除或弃用的代码开始,观察会发生什么。@johncode什么代码是过时的?很抱歉,我是php新手,刚刚学习了所有这些。看到这些被删除或弃用的代码,我的眼睛都在流血。。我必须说,请停止使用PHP4:-|@Aniruddhacharaborty我很抱歉。我一定有一本我正在用的旧书。我需要做些什么才能使它更现代?对于初学者,您需要在
HTTP\u POST\u VARS
索引周围加引号,如$HTTP\u POST\u VARS['username']
。但是,你应该使用$\u POST($\u POST['username']
)。我已经编辑了我的原始帖子。这当然有帮助,但我肯定在某些地方仍有错误。好吧,您只需按照验证消息所示,用有效输入填写您的表格。我是。我遵循这些标准。它将用户名和电子邮件保留在已填写的字段中,但我必须不断输入密码,据我所知,当我点击submit.chekout时,没有发生任何事情。这不正确吗?我想我会使用post并将其设置为register.php?