我試圖創建一個可以檢查登錄字段並驗證用戶名和密碼的php文件(login.php)。目前,它只是使用正則表達式來檢查用戶名。以下是我的代碼。我無法觸發第二個else子句,看起來isset和!empty總是如此,即使我看到它們在視覺上是空的,就像在刷新頁面之後立即看到的那樣。isset和!empty總是爲登錄字段返回true
<?php
if (isset($_POST['login']) && !empty($_POST['login'])) {
if (preg_match('/^[a-z0-9]{6,15}$/i', $_POST['username'])) {
echo 'set';
}
else {
echo 'wrong';
}
} else {
echo 'required field';
}
?>
<div id="login"> <!-- Login field with link to registration -->
<form method="POST" action="login.php">
<Legend>Login</Legend>
Username <input type="text" name="username"/>
Password <input type="password" name="password"/>
<input type="submit" name="login">
<div id="register">
<a href="registration.html">Not a member? Click here to register!</a>
</div>
</form>
</div>
並且'var_dump($ _ POST ['login'])'顯示什麼? –
'isset'和'empty'有點矯枉過正[看手冊](http://php.net/manual/en/function.empty.php) – RiggsFolly
看起來你似乎在重新提交表單。 –