2015-10-11 99 views
-1

我正在註冊表單上,並且遇到問題。無論我輸入表單中的字段,只要我點擊提交,表單就會清除,我永遠不會轉發到homepage.php文件。有沒有人看到什麼導致這個文件不被讀取?謝謝!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:]]|_|\.|-)[email protected]([[: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:這裏是homepage.php的,你必須使用新的資源,瞭解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> 
+2

你的代碼是過時的並已爲*年*。首先編寫尚未刪除或棄用的代碼,並觀察發生的情況。 –

+0

@JohnConde什麼代碼已過時?對不起,我對PHP很陌生,只是學習了這一切。 – still2blue

+1

我的眼睛在看到這個刪除或不贊成的代碼* _ *之後出血。我必須說,請停止使用PHP 4: - | –

回答

1

首先,因爲你好像從一本舊書或教程學習。使用

$ _POST 

代替

$HTTP_POST_VARS 

使用也

preg_match() 

代替

eregi() 

個這些資源也許可以幫助你達到你的目標

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

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

+0

我編輯了我原來的帖子。這當然有幫助,但我仍然必須在某個地方有錯誤。 – still2blue

+0

那麼,您只需在驗證信息顯示時填寫有效的輸入。 –

+0

我是。我遵循標準。它將用戶名和電子郵件保存在已填寫的字段中,但我必須不斷輸入密碼,並且據我所知,在我點擊提交時沒有任何事情發生。 – still2blue