In today’s ever-expanding digital landscape, mastering how to create a simple login form or a complete login and registration form is essential for any web developer. Whether you’re coding from scratch in PHP and MySQL or designing a polished interface with HTML and CSS, this guide will cover:
- Creating a login form template with HTML and CSS
- Implementing a PHP login page and PHP login form with MySQL
- Combining login and registration in one login registration form
- Optimizing for SEO, security, and best practices
- Frequently Asked Questions (FAQ)
By the end of this comprehensive article, you’ll have a polished, secure, and responsive login page code ready for deployment.
Part 1: Simple Login Form with HTML and CSS
Let’s begin with a simple login page built using HTML and CSS.
HTML: Basic Login Form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Login Form</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="login-container">
<form action="login.php" method="post" class="login-form">
<h2>User Login</h2>
<div class="input-group">
<label for="username">Username</label>
<input type="text" id="username" name="username" required />
</div>
<div class="input-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" required />
</div>
<button type="submit">Login</button>
<p>Don't have an account? <a href="register.html">Register here</a></p>
</form>
</div>
</body>
</html>
CSS: Stylish CSS Login Design
/* styles.css */
body {
background: #f2f2f2;
font-family: Arial, sans-serif;
}
.login-container {
width: 360px;
margin: 100px auto;
background: #fff;
padding: 40px;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.login-form h2 {
margin-bottom: 20px;
text-align: center;
}
.input-group {
margin-bottom: 20px;
}
.input-group label {
display: block;
margin-bottom: 5px;
}
.input-group input {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
width: 100%;
background: #4CAF50;
color: white;
padding: 10px;
border: none;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background: #45a049;
}
.login-form p {
text-align: center;
margin-top: 15px;
}
This login form template includes a clean design and responsive layout. You can integrate simple login page features like “Remember Me” or “Forgot Password” as needed.
Part 2: Secure PHP Login Form with MySQL Database
Building a secure PHP login form involves handling user credentials, validating input, and interacting with a database.
Step 1: Database Setup
Run this SQL to create a users table:
CREATE DATABASE webdev_demo;
USE webdev_demo;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 2: User Registration Form in PHP (register.php
)
<?php
// register.php
$conn = new mysqli('localhost', 'root', '', 'webdev_demo');
if ($conn->connect_error) die('Connection error');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $conn->real_escape_string($_POST['username']);
$email = $conn->real_escape_string($_POST['email']);
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
$stmt = $conn->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $username, $email, $password);
if ($stmt->execute()) {
header('Location: login.html?registered=1');
exit;
} else {
echo "Error: " . $conn->error;
}
}
?>
Registration HTML: Easy Login and Registration Form
<form action="register.php" method="post">
<h2>Register</h2>
<label>Username</label>
<input type="text" name="username" required />
<label>Email</label>
<input type="email" name="email" required />
<label>Password</label>
<input type="password" name="password" required />
<button type="submit">Register</button>
</form>
Step 3: PHP Login Page Code (login.php
)
<?php
session_start();
$conn = new mysqli('localhost', 'root', '', 'webdev_demo');
if ($conn->connect_error) die('Connection error');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $conn->real_escape_string($_POST['username']);
$password = $_POST['password'];
$stmt = $conn->prepare("SELECT id, password FROM users WHERE username=?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($userId, $hashedPassword);
$stmt->fetch();
if (password_verify($password, $hashedPassword)) {
$_SESSION['user_id'] = $userId;
$_SESSION['username'] = $username;
header('Location: dashboard.php');
exit;
} else {
$error = "Invalid credentials";
}
} else {
$error = "User not found";
}
}
?>
Dashboard (dashboard.php
)
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.html');
exit;
}
?>
<!DOCTYPE html>
<html>
<head><title>Dashboard</title></head>
<body>
<h1>Welcome, <?= htmlspecialchars($_SESSION['username']) ?>!</h1>
<a href="logout.php">Logout</a>
</body>
</html>
Logout (logout.php
)
<?php
session_start();
session_destroy();
header('Location: login.html');
exit;
?>
This login page code sets up a full authentication flow: registration, login, dashboard, and logout. It’s a solid starting point for many web apps.
Part 3: Enhancing UX & Security
Adding “Remember Me”
<input type="checkbox" id="remember" name="remember" />
<label for="remember">Remember me</label>
In login.php
, check the remember
flag, and set a secure, HttpOnly cookie.
Sanitizing & Validating Data
Always sanitize with htmlspecialchars()
or filter_input()
, and validate email formats:
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "Invalid email format";
}
Password Security
Use PASSWORD_ARGON2ID
when available:
$password = password_hash($_POST['password'], PASSWORD_ARGON2ID);
CSRF Protection
Include a CSRF token in forms and session-check on submit.
Part 4: Login Form Template Components
Responsive Design
Add media queries:
@media (max-width: 480px) {
.login-container {
width: 90%;
margin: 50px auto;
}
}
Visual Variants
- Modern gradient background
- Material Design form fields
- Tooltip validation messages
Accessibility Tips
- Use
label for="..."
- Add
aria-label
attributes - Ensure keyboard navigability
Part 5: SEO Best Practices
- Use semantic HTML:
<form>
,<label>
,<main>
,<section>
- Ensure fast loading: minify CSS, lazy-load images
- Mobile-friendly forms for improved mobile-first SEO
- Use
title
,meta description
, andlang="en"
- Include alt text for illustrations
FAQs
What is the difference between a login form and a registration form?
A login form allows existing users to authenticate, while a registration form enables new users to create an account. A login registration form that combines both requires logic to switch between modes or direct users accordingly.
How do I secure a PHP login form?
Use password_hash()
and password_verify()
for password safety; sanitize inputs, leverage prepared statements, add CSRF tokens, and set secure session/cookie flags.
Can I create a login form with only HTML and CSS?
Design-wise, yes. Functionality-wise, no—the form needs a PHP login form, JavaScript, or a backend server to process credentials.
What is the “Remember Me” option in login forms?
It stores a long-lived cookie so the user stays logged in across sessions. Always implement this with secure token storage and renewal logic.
How do I validate login credentials on the server side?
Instead of $_POST['username']
, use $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
and compare inputs to your database values.
Do login forms affect SEO?
Not directly. However, secure (HTTPS), fast, and mobile-responsive login forms contribute to overall site quality, which search engines consider.
Can I use JavaScript to submit a login form?
Yes, via AJAX, but always pair with robust server-side validation. Don’t rely solely on client-side checks.
Leave a Reply