<分区>
我正在尝试使我的 php 脚本 protected 页面,因此在添加登录名时我希望它根据数据库用户名和密码检查用户名和密码输入,如果它们匹配,它将允许他们查看 protected 页面直到注销。我已经能够根据虚拟登录用户名和密码设置 protected 页面和注销,现在我想将它连接到数据库用户。这是我的 html 和 php 代码。任何帮助将不胜感激:)
HTML:
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="post" action="login.php">
<table border="1" cellpadding="2">
<caption>
Staff Login
</caption>
<tr>
<td><label for="username">Username</label></td>
<td><input type="text" name="username" cols="45" rows="5" tabindex="2"/></td>
</tr>
<tr>
<td><label for="password">Password</label></td>
<td><input type="password" name="password" cols="45" rows="5" tabindex="2"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login" /></td>
</tr>
</table>
</form>
</body>
</html>
PHP:
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
// Check if username and password are correct
if ($_POST["username"] == 'username' && $_POST["password"] == 'password') {
include 'connection.php';
// If correct, we set the session to YES
session_start();
$_SESSION["Login"] = "YES";
echo "You are now logged in.</br></br>";
echo "<a href='list-contacts.php'>Proceed to contacts...</a>";
}
else {
// If not correct, we set the session to NO
session_start();
$_SESSION["Login"] = "NO";
echo "Incorrect username and / or password.</br></br>";
echo "<a href='login.html'>Return to login</a>";
}
?>
</body>
</html>