2013-03-08 81 views
0

我有一個表單,它有兩個值,user和pass提交時。用php關聯數組檢查bool值

$_POST['submit'] = the name of the submit button is "submit" 

當用戶提交我在PHP下面的腳本來驗證:

$logins = array(
    'user1' => 'pass1', 
    'user2' => 'pass2', 
    'user3' => 'pass3' 
); 

foreach($_POST as $key => $value) { 
    $_POST[$key] = stripslashes($_POST[$key]); 

    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key])); 
} 

/******************************************************************************/ 
    if (isset($_POST['submit'])){ 

     $user = isset($_POST['user']) ? strtolower($_POST['user']) : ''; 
     $pass = isset($_POST['pass']) ? $_POST['pass'] : ''; 
     $report = $_POST['typereport']; 

     if ($logins[$user] != $pass) { 
     showForm("Wrong Username/Password"); 
     exit();  
     } 
     else { 
    if ($report == "Clinical") { 
     $file = $filename; 
     $contents = file($file); 
     $string = implode("<br>", $contents); 
     echo "<head><title>ScoreViewer: Clinical</title></head>"; 
     echo "Logged in as: " . strtoupper($user) . "<br>"; 
     echo "<a href='log2.php'>Sign Out</a>"; 
     echo "<br><br>"; 
     echo "<pre>" . $string . "</pre>"; 
     echo "<br><br>"; 
    } 
    elseif ($report == "Non-Clinical") { 
     $file = $filename2; 
     $contents = file($file); 
     $string = implode("<br>", $contents); 
     echo "<head><title>ScoreViewer: Non-Clinical</title></head>"; 
     echo "Logged in as: " . strtoupper($user) . "<br>"; 
     echo "<a href='log2.php'>Sign Out</a>"; 
     echo "<br><br>"; 
     echo "<pre>" . $string . "</pre>"; 
     echo "<br><br>"; 
    } 
     } 
    } else { 
     showForm(); 
     exit(); 
    } 

現在會發生時,該腳本將比較用戶名和密碼進入找到匹配的內容。如果找到匹配項並且基於它的報告類型,它將顯示。但由於某種原因,每當提交按鈕被按下時,它直接進入IF聲明的臨牀部分。

如果我只使用一個沒有數組的用戶名/密碼,它可以正常工作。如下所示:

$username = "user"; 
$password = "pass"; 

foreach($_POST as $key => $value) { 
    $_POST[$key] = stripslashes($_POST[$key]); 

    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key])); 
} 

/******************************************************************************/ 
    if (isset($_POST['submit'])){ 

     $user = isset($_POST['user']) ? strtolower($_POST['user']) : ''; 
     $pass = isset($_POST['pass']) ? $_POST['pass'] : ''; 
     $report = $_POST['typereport']; 

     if ($user != $username && $pass != $password) { #$logins[$user] != $pass) { 
     showForm("Wrong Username/Password"); 
     exit();  
     } 
     else { 
// decide what to do here if the user and pass is correct, deleted to save space 
     } 
    } else { 
     showForm(); 
     exit(); 
    } 

如何實現我正在嘗試完成的操作?

完整的HTML代碼:Pastbin

+0

在你原來的例子,後續代碼var_dump( $登錄[$用戶]);在條件之前,並驗證它是否與$ pass相同。 – mkaatman 2013-03-08 22:18:57

+0

我的提交按鈕: Si8 2013-03-08 22:21:39

+0

只有當我有一個數組時纔會發生。但事實是,它不是沒有正確驗證,在Internet Explorer中,它完全跳過它,甚至不檢查 – Si8 2013-03-08 22:22:18

回答

2
if ($logins[$user] != $pass) {// there lies error one... 

$用戶可能無法在$登錄陣列的關鍵,與array_key_exists首先檢查。

然後將代碼顯示的 「其他」 S濫用...

 showForm("Wrong Username/Password"); 
    exit();     // That condition exits 
    } 
     else {     // So you do not need this else and the block. 

所以重新寫它:

$logins = array(
    'user1' => 'pass1', 
    'user2' => 'pass2', 
    'user3' => 'pass3' 
); 

foreach($_POST as $key => $value) { 
    $_POST[$key] = stripslashes($_POST[$key]); 
    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key])); 
} 
/******************************************************************************/ 
if (isset($_POST['submit'])){ 
    $user = isset($_POST['user']) ? strtolower($_POST['user']) : ''; 
    $pass = isset($_POST['pass']) ? $_POST['pass'] : ''; 
    $report = $_POST['typereport']; 
    if ((!array_key_exists($user, $logins))||($logins[$user] != $pass)) { 
     showForm("Wrong Username/Password"); 
     exit();  
    } 
    if($report == "Clinical") { 
     $file = $filename; 
     $contents = file($file); 
     $string = implode("<br>", $contents); 
     echo "<head><title>ScoreViewer: Clinical</title></head>"; 
     echo "Logged in as: " . strtoupper($user) . "<br>"; 
     echo "<a href='log2.php'>Sign Out</a>"; 
     echo "<br><br>"; 
     echo "<pre>" . $string . "</pre>"; 
     echo "<br><br>"; 
    } 
      // No need if you only have only clinic and non clinic reports... 
      // elseif ($report == "Non-Clinical") { 
    else { // now non clinic 
     $file = $filename2; 
     $contents = file($file); 
     $string = implode("<br>", $contents); 
     echo "<head><title>ScoreViewer: Non-Clinical</title></head>"; 
     echo "Logged in as: " . strtoupper($user) . "<br>"; 
     echo "<a href='log2.php'>Sign Out</a>"; 
     echo "<br><br>"; 
     echo "<pre>" . $string . "</pre>"; 
     echo "<br><br>"; 
    } // this block ends non clinic - clinic choices... 
}  // this ends the if (isset($_POST['submit'])) 
else { // and this does if !isset($_POST['submit']) 
    showForm(); 
    exit(); 
} 

希望這有助於

+0

感謝您的反饋。我會測試和更新 – Si8 2013-03-11 15:03:37

+0

像魅力一樣工作......謝謝!!!!! :) – Si8 2013-03-11 15:06:35

+0

你很好。 – Ihsan 2013-03-12 18:51:23