尽管在这个网站上检查了这个主题,但我无法弄清楚出了什么问题:我开发了一个 html 表单和一个 php 文件来处理这个表单 - 请参见下面的代码(事实上,我直接从http的示例中复制了大部分代码://www.phpro.org/tutorials/Basic-Login-Authentication-with-PHP-and-MySQL.html)。
问题:当我没有填写必填字段(用户名或密码)时,PHP 代码应显示“请输入有效的用户名和密码”,但似乎代码开头的 isset() 函数也返回 TRUE未填写必填字段时?(Ee,代码指示以下错误消息''Incorrect Length for Username'')
请您的帮助。先感谢您。
html 表单:"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> PHPRO 登录
<body>
<h2>Login Here</h2>
<form action="login_submit1.php" method="post">
<fieldset> <p> <label for="username">Username</label> <input type="text" id="username"
name="username" value="" maxlength="20" /> </p>
<p> <label for="password">Password</label> <input type="text" id="password"
name="password" value="" maxlength="20" /> </p> <p> <input type="submit" value="→ Login"
/> </p> </fieldset> </form>
</body>
</html>
...以及 php 文件的相关部分:
/*** begin our session ***/
session_start();
//! include lib after session_start()
include 'test_ecis_lib_pdo.php';
/*** check if the users is already logged in ***/
if(isset( $_SESSION['user_id'] ))
{
$message = 'Users is already logged in';
}
/*** check that both the username, password have been submitted ***/
****if(!isset( $_POST['username'], $_POST['password']))
{
$message = 'Please enter a valid username and password';
}
/*** check the username is the correct length ***/
elseif (strlen( $_POST['username']) > 20 || strlen($_POST['username']) < 4)
{
$message = 'Incorrect Length for Username';
}
/*** check the password is the correct length ***/
elseif (strlen( $_POST['password']) > 20 || strlen($_POST['password']) < 4)
{
$message = 'Incorrect Length for Password';
}