Login Page
Today we will make login page for our application.
Login page, ask user to enter user name (in our case, we use email as user name) and password. When user click "Login" button, data is sent to PHP program (login.php) using POST method.
login.php will read email and password from $_POST variable, check the database for user with email address provided by user. If a user is found, we read the information of the user and check if user entered password is same as the password stored in database.
login.html
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="login.php" method="post">
Email:
<input type="text" name="email">
<br>
Password:
<input type="text" name="password">
<br>
<button type="submit" name="submit">Login</button>
</form>
</body>
</html>
This is simple HTML form as we used in other pages like signup.html, lost_password.html
login.php
PHP Code:
<?php
require 'includes/database.php';
// lets read email and password from $_POST
// array.
$email = $_POST['email'];
$password = $_POST['password'];
// Lets remove any white space (space, tabs, newline etc..)
$email = trim($email);
$password = trim($password);
// Lets find user with email address $email
$sql = "select * from users where email='$email'";
$result = $mysqli->query($sql);
if ($result->num_rows == 0) {
die("No user with email address $email found");
}
// We have a result, so lets get that user details
$userInfo = mysqli_fetch_assoc($result);
// lets check if password in database match the user entered password.
if ($userInfo['password'] == $password) {
echo "Password Matched";
} else {
die("Invalid password");
}
We get message "Password Matched" if user entered password is same as password stored in database.
Bookmarks