The best magazine
How to Build a Website With a Login
- 1). Create an initial HTML page that will contain your form. For now, call it "index.html."
- 2). Inside index.html, add the following code:
<html>
<body>
<form action="login.php" method="POST">
User Name: <input type="text" name="user_name" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" value="Login" />
</form>
</body>
</html>
The form's action, login.php, is where the form's data will be sent when the user clicks the "Login" button. - 3). Create a new file in the same folder as your index.html file, and call it "login.php." Enter the following text in login.php:
<?php
$userName = $_POST["user_name"];
$password = $_POST["password"];
if (($userName == "testuser") && ($password == "testpass")) {
$loggedIn = true;
} else {
$loggedIn = false;
}
?>
<html>
<body>
<?php if ($loggedIn) { ?>
Welcome! You've successfully logged into the system!
<?php } else { ?>
Sorry, your credentials were not valid. Please use your browser's back button to try again.
<?php } ?>
</body>
</html>
To test this, load index.html in your browser and enter in a username and password. For a successful login attempt, use "testUser" for your username and "testpass" for your password (no quotes on either); use anything else to see an invalid login attempt.