2015-10-05 330 views
0

要完成我的網站,我想添加一個登錄功能。一切運行良好,然後我決定添加一些PHP驗證的形式。大多數編碼是由Dreamweaver本身完成的,但是我添加了驗證if-questions,這就是錯誤所在。 每次我填寫表格時,我都會收到一個錯誤消息,說我的密碼需要8個字符,不管它有多少個字符。如果我擺脫了這個if-question,那就是說錯誤變量是未定義的。如果我然後用isset()修復這個問題,它似乎跳過以下兩個if-questions。 我希望我能得到我的觀點清晰明瞭,並很期待的迴應,因爲我得到一點點沮喪,現在這個代碼:PPHP驗證失敗

感謝, 揚

if (!function_exists("GetSQLValueString")) { 
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{ 
    if (PHP_VERSION < 6) { 
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 
    } 

    $theValue = function_exists("mysql_real_escape_string") ?mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 

    switch ($theType) { 
case "text": 
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
    break;  
case "long": 
case "int": 
    $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
    break; 
case "double": 
    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; 
    break; 
case "date": 
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
    break; 
case "defined": 
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
    break; 
    } 
    return $theValue; 
} 
} 

// *** Redirect if username exists 
$MM_flag="MM_insert"; 
if (isset($_POST[$MM_flag])) { 
    $MM_dupKeyRedirect="registrationFailed.php"; 
    $loginUsername = $_POST['email']; 
    $LoginRS__query = sprintf("SELECT Email FROM `Start-Login` WHERE Email=%s", GetSQLValueString($loginUsername, "text")); 
    mysql_select_db($database_login, $login); 
    $LoginRS=mysql_query($LoginRS__query, $login) or die(mysql_error()); 
    $loginFoundUser = mysql_num_rows($LoginRS); 

    //if there is a row in the database, the username was found - can not add the requested username 
    if($loginFoundUser){ 
    $MM_qsChar = "?"; 
//append the username to the redirect page 
if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&"; 
$MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername; 
header ("Location: $MM_dupKeyRedirect"); 
exit; 
    } 
} 

$editFormAction = $_SERVER['PHP_SELF']; 
if (isset($_SERVER['QUERY_STRING'])) { 
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); 
} 

if (empty($_POST) === false) { 
$required_fields = array('name', 'email', 'password', 'passwordconfirm'); 
foreach ($_POST as $key=>$value) { 
    if (empty($value) && in_array($key, $required_fields) === true) { 
    $errors[] = 'Please fill out all requiered fields'; 
    $lul = true; 
    break 1; 
    } 
} 

} 

if (empty($errors) === true) { 
if (strlen(isset($_POST['password'])) < 8) { 
    $errors[] = 'Your password must be at least 8 characters long'; 
    $lil = true; 
} 
if (isset($_POST['password']) !== isset($_POST['passwordconfirm'])) { 
    $errors[] = 'Your passwords do not match'; 
    $lil = true; 
} 
if (filter_var(isset($_POST['email']), FILTER_VALIDATE_EMAIL)) { 
    $errors[] = 'You must provide a valid email address'; 
    $lil = true; 
} 
} 

isset($errors); 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form") && (isset($lul) === false) && (isset($lil) === false)) { 
    $insertSQL = sprintf("INSERT INTO `Start-Login` (Email, Password, Name, `role`) VALUES (%s, %s, %s, %s)", 
        GetSQLValueString($_POST['email'], "text"), 
        GetSQLValueString($_POST['password'], "text"), 
        GetSQLValueString($_POST['name'], "text"), 
        GetSQLValueString($_POST['role'], "text")); 

    mysql_select_db($database_login, $login); 
    $Result1 = mysql_query($insertSQL, $login) or die(mysql_error()); 

    $insertGoTo = "login.php"; 
    if (isset($_SERVER['QUERY_STRING'])) { 
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; 
$insertGoTo .= $_SERVER['QUERY_STRING']; 
    } 
    header(sprintf("Location: %s", $insertGoTo)); 
} else { 
echo '<pre>', print_r($_POST, true), '</pre>'; 
echo '<pre>', print_r($errors, true), '</pre>'; 
} 

回答

1
if (strlen(isset($_POST['password'])) < 8) { 

會嘗試strlen的上和布爾 所以儘量

if(isset($_POST['password'] && strlen($_POST['password']) < 8) { 

而且這樣做

if (isset($_POST['password']) !== isset($_POST['passwordconfirm'])) { 

裏面做檢查,如果密碼是平等的,它檢查是否有一組和一個不 這應該是這樣的

if (isset($_POST['password'],$_POST['passwordconfirm']) 
    && $_POST['password'] !== $_POST['passwordconfirm']) { 

同樣在這裏

if (filter_var(isset($_POST['email']), FILTER_VALIDATE_EMAIL)) { 

應該是

if (isset($_POST['email']) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { 

您可以查看文檔 isset(),strlen()

+0

感謝您的快速響應。它解決了密碼長度和確認問題,但是,如果我輸入了一個無效的電子郵件地址,它仍然無關緊要:/ ...此外,它現在不會給我一個錯誤消息,告訴我出了什麼問題,而只是放置出「1」? – ProBeastC

+0

你在我們的代碼中有一個隨機的''isset($ errors);''你可能想要一個''if(..)'' – jmattheis

+0

謝謝,解決了錯誤信息的問題, ,由於某種原因電子郵件驗證仍然無法正常工作,這是最大的問題,因爲我將使用它作爲用戶ID .... – ProBeastC