Login page allows the registered user khổng lồ access the website và manage its account by entering its username và password.Bạn đã xem: Create a registration and login system with php & mysql
User SESSION is been created on a successful login attempt.
Bạn đang xem: Php tutorial: extract & compact methods
This helps to detect the user is actually logged in or not when going lớn other website pages and it also helps lớn display nội dung according to lớn the user.
In this tutorial, I show how you can create a Login page with PHP & MySQL.

Contents
1. Table structure
I am using users table in the tutorial example.
CREATE TABLE `users` ( `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `username` varchar(80) NOT NULL, `name` varchar(80) NOT NULL, `password` varchar(80) NOT NULL) ENGINE=InnoDB mặc định CHARSET=latin1;
2. Configuration
Create a new config.php file for database configuration.Completed Code
3. HTML
Completed Code
4. CSS
/* Container */.container width:40%; margin:0 auto;/* Login */#div_login border: 1px solid gray; border-radius: 3px; width: 470px; height: 270px; box-shadow: 0px 2px 2px 0px gray; margin: 0 auto;#div_login h1 margin-top: 0px; font-weight: normal; padding: 10px; background-color: cornflowerblue; color: white; font-family: sans-serif;#div_login div clear: both; margin-top: 10px; padding: 5px;#div_login .textbox width: 96%; padding: 7px;#div_login input padding: 7px; width: 100px; background-color: lightseagreen; border: 0px; color: white;5. PHP
On size submit check the username and password entered or not. If entered then execute a SELECT query khổng lồ count records on the basis of username & password.Xem thêm: Cách Lên Bảng Ngọc Phép Liên Quân, Mùa 19 Mạnh Nhất 2021
If the query returns greater than 0 then initialize $_SESSION = $uname & redirect to lớn home.php.
If it returns 0 then display the "Invalid username và password message".
Complete Code
0) $_SESSION = $uname; header("Location: home.php"); else echo "Invalid username & password"; }}
Homepage
Create new home.php file.First, check whether the user is logged or not by checking the $_SESSION variable. If it not mix then redirect the user to lớn index.php for login.
Created a logout button that destroy the $_SESSION variable and redirect to index.php web page on click.