![](/img/trans.png)
[英]PHP User login validation using login.php included in index.php not working
[英]Why is my validation from files index.php to login.php not working?
我要嘗試做的是確保用戶在單擊“提交”之前輸入了數量。 問題在於,用戶可以繼續登錄屏幕,而無需在數量框中輸入任何數字。 我曾嘗試粘貼驗證碼,但仍將我的用戶帶到login.php屏幕。 任何幫助將不勝感激!
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form action='login.php' method='post'>
<?php
//The following arrays contain my products and their information, one product per array.
$hulkhamburger = array('Food' => 'Hulk Hamburger', 'Description' => '...', 'Price' => '$1.00', 'Quantity' => '<input type="text" name="quantity">');
$atomichotdog = array('Food' => 'Atomic Hot Dog', 'Description' => '...', 'Price' => '$2.00', 'Quantity' => '<input type="text" name="quantity">');
$friedchicken = array('Food' => 'Fantastic 4 Fried Chicken', 'Description' => '...', 'Price' => '$3.00', 'Quantity' => '<input type="text" name="quantity">');
$psyonicpizza = array('Food' => 'Psyonic Pizza', 'Description' => '...', 'Price' => '$4.00', 'Quantity' => '<input type="text" name="quantity">');
$marvelmeatloaf = array('Food' => 'Marvel Meatloaf', 'Description' => '...', 'Price' => '$5.00', 'Quantity' => '<input type="text" name="quantity">');
//The following array takes my previous five arrays and puts them into one array for easier coding and reading.
$allfood = array ($hulkhamburger, $atomichotdog, $friedchicken, $psyonicpizza, $marvelmeatloaf);
/*The following code centers my table on the page, makes the table background white,
makes the table 50% of the browser window, gives it a border of 1 px,
gives a padding of 2 px between the cell border and content, and gives 1 px of spacing between cells.
*/
echo "<table align=center bgcolor='FFFFFF' width=50% border=1 cellpadding=1
cellspacing=2>";
/*The following code prints my table header.
* Credit goes to Dr. Kazman; code is from Lecture 10.
* I used his code because it was easier (and a more efficient way) than doing it all manually like I did for Mini Asst. 2.
*/
echo "<tr>";
$header = array_keys($hulkhamburger);
foreach ($header as $key => $myheader)
{
echo "<th>$myheader</th>";
}
echo "</tr>";
//The following code loops through the whole table body and then prints each row.
for($i=0; $i<count($allfood); $i++)
{
echo "<tr>";
foreach ($allfood[$i] as $key => $value)
{
echo ("<td align=center>$value</td>");
}
}
//This code ends my table.
echo "</align>";
echo "<br>";
?>
<tr>
<td>
<td>
<td>
<td>
<center><input type='submit' value='submit'></center>
</form>
</body>
</html>
這是我的login.php
<?php//made the user login menu into a nice table that will center the username and password in the middle of the page.?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<form name="login" method="post" action="invoice.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1">
<tr>
<td colspan="3"><strong>User Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type='submit' name='login' value='Login'>
<?php
/*This code will allow a new user to go to the registration page and register for the site
* before buying anything.
*/
?>
<a href="registration.php"><br>New user registration</a>
</td>
</tr>
</table>
</td>
</form>
</tr>
</table>
您將用戶移動了
header("Location: http://www.google.de");
您在表單中指定的頁面必須進行驗證。 您可以將用戶直接發送到login.php,如果驗證失敗,則將其發送回;或者,如果驗證成功,則由index.php進行驗證,並將用戶發送到login.php,或者您創建第三個發送腳本用戶進入兩個頁面之一。
如果要在將值傳遞到服務器之前檢查它們,請使用Javascript。
如果您不想自己編寫JS,請使用框架(例如YII)。
我沒有在您的第一頁上看到任何驗證代碼,這實際上是您應該進行驗證的位置-在用戶甚至可以轉到下一頁之前。 這是一個好/簡單的例子。
如果您只是想防止人們根據輸入元素的值單擊按鈕,則可以執行以下操作:
<script>
function validateQuantity() {
var x = document.getElementById('quantity');
var y = document.getElementById('submitBtn');
if(isNumber(x.value)){
y.disabled = false;
}else{
y.disabled = true;
}
}
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
這將切換“提交”按鈕的“禁用”屬性。 我承認,這不是最佳答案。 您實際上應該在表單頁面上進行表單驗證,並通過以下代碼觸發JS驗證代碼: onsubmit="return(validate());"
在表單元素中。
然后,您可以在接收$_POST
數據的頁面上再次驗證結果,以防止發生任何此類的注入和填充。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.