我有一個表單,它有兩個值,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
在你原來的例子,後續代碼var_dump( $登錄[$用戶]);在條件之前,並驗證它是否與$ pass相同。 – mkaatman 2013-03-08 22:18:57
我的提交按鈕: – Si8 2013-03-08 22:21:39
只有當我有一個數組時纔會發生。但事實是,它不是沒有正確驗證,在Internet Explorer中,它完全跳過它,甚至不檢查 – Si8 2013-03-08 22:22:18