- Get link
- X
- Other Apps
Blog Post: How to Create a Login System in PHP (Step-by-Step with Code)
๐น Introduction
Creating a login system is one of the most common and essential features of any web application. In this tutorial, you’ll learn how to build a simple and secure login system using PHP and MySQL.
Whether you're a beginner or refreshing your skills, this step-by-step guide will help you understand the core concepts behind user authentication.
๐ง Technologies Used:
-
PHP
-
MySQL (phpMyAdmin)
-
HTML & CSS (Basic)
๐ Folder Structure
/login-system
├── db.php
├── login.php
├── welcome.php
├── logout.php
๐ Step 1: Create the Database and Table
Open phpMyAdmin and run the following SQL query:
CREATE DATABASE login_demo;
USE login_demo;
CREATE TABLE users (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
);
Now insert a test user:
INSERT INTO users (username, password)
VALUES ('admin', MD5('admin123'));
๐ Step 2: Create Database Connection (db.php)
$host = "localhost";
$user = "root";
$password = "";
$database = "login_demo";
$conn = mysqli_connect($host, $user, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
๐ Step 3: Create Login Form (login.php)
<?php
session_start();
include "db.php";
$error = '';
if (isset($_POST['login'])) {
$username = $_POST['username'];
$password = md5($_POST['password']); // MD5 encryption
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) == 1) {
$_SESSION['username'] = $username;
header("Location: welcome.php");
} else {
$error = "Invalid username or password!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Login System</title>
</head>
<body>
<h2>Login Form</h2>
<form method="post">
<input type="text" name="username" required placeholder="Username"><br><br>
<input type="password" name="password" required placeholder="Password"><br><br>
<button type="submit" name="login">Login</button>
</form>
<p style="color:red;"><?php echo $error; ?></p>
</body>
</html>
๐ Step 4: Welcome Page (welcome.php)
<?php
session_start();
if (!isset($_SESSION['username'])) {
header("Location: login.php");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Welcome, <?php echo $_SESSION['username']; ?>!</h2>
<a href="logout.php">Logout</a>
</body>
</html>
๐ Step 5: Logout (logout.php)
<?php
session_start();
session_destroy();
header("Location: login.php");
exit();
?>
✅ Conclusion
You’ve now built a working login system using PHP and MySQL!
In a real-world project, you should use password hashing (password_hash
) instead of MD5 for better security.
Next steps:
-
Add registration page
-
Use
password_hash()
andpassword_verify()
-
Implement "remember me" functionality
๐ Related Posts (for interlinking):
-
How to connect PHP with MySQL database
-
PHP CRUD tutorial for beginners
-
PHP form validation step-by-step
๐ SEO Keywords You Can Use (for Blogger)
-
how to create login system in php
-
php mysql login script
-
simple php login form
-
user authentication php
-
php login system tutorial for beginners
Aap chahe to is post ko apne Blogger me copy-paste karke publish kar sakte ho.
Agar aap chaho to main:
-
HTML-ready version
-
Syntax-highlighted format
-
Blogger me import file bhi de sakta hoon
Batao:
✅ Is post ko Blogger me paste karne ke liye HTML version chahiye
Ya:
✍ Agla post PHP registration system par banao
- Get link
- X
- Other Apps
Comments
Post a Comment