How to Create a Simple Login With PHP
- 1). Using your HTML editor or notepad, create a new Web page and save it as "login.php". Create a form and use the "post" method value. Example code follows:
<form action="login.php" method="post">
</form> - 2). Add two input boxes within the form for the username and password fields. Use the "password" type for the password box. Example code follows:
<form action="login.php" method="post">
<input type="text" name="username">
<input type="password" name="password">
</form> - 3). Add a submit button which will be used to submit the form. Example code follows:
<input type="submit" value="Submit"> - 1). Declare two variables that will hold the values from the form submission. Call them "username" and "password". Example code follows:
<?php
$username = $_POST['username']
$password = $_POST['password']
?> - 2). Write an "if" statement to check if the form has been submitted. The code for this is:
if($_SERVER['REQUEST_METHOD'] == "POST") {
} - 3). Write an "if" statement to check the username and password against two values of your choice. Include the "header" statement to redirect the user accordingly. Example code follows:
if ($username == "user" && $password == "pass"){
header('Location: http://www.example.com/success.html');
die();
else
{
header('Location: http://www.example.com/loginfailed.html');
die();
}
Input Form
PHP Code
Source...