PHP Login System Requirements:
- We should have knowledge of HTML, CSS, PHP and MySQL for
creating the login system.
- Text Editor - For writing the code. We can use any text editor
such as Notepad, Notepad++, Dreamweaver, etc.
- XAMPP - XAMPP is a cross-platform software, which stands for
Cross-platform(X) Apache server (A), MySQL (M), PHP (P), Perl (P).
XAMPP is a complete software package, so, we don't need to install
all these separately.
Setup
Now, we need to start the webserver and create the files for the
login system. There are a few steps that are given below to set up
the environment.
- Open the XAMPP Control Panel.
- Start the Apache server by clicking on the Start button.
- Start the MySQL by clicking on the Start button.
- Create all the files needed for login.
- Create login table in the database using phpMyAdmin in
XAMPP.
Now, we will create four files here for the login
system.
- index.html:This file is created for the GUI
view of the login page and empty field validation.
- style.css: This file is created for the
attractive view of the login form.
- connection.php: Connection file contains the
connection code for database connectivity.
- authentication.php:This file validates the
form data with the database which is submitted by the user.
Save all these files in the htdocs folder inside the
Xampp installation folder. The step by step description and code
for these files are discussed below.
Step 1: Creating the Database Table
CREATE TABLE users (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
Step 2: Creating the Config File
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'demo');
/* Attempt to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD,
DB_NAME);
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
Step 3: Creating Login Page
- <html>
- <head>
- <title>PHP login
system</title>
- // insert style.css file inside
index.html
- <link rel = "stylesheet" type =
"text/css" href = "style.css">
- </head>
- <body>
- <div id = "frm">
-
<h1>Login</h1>
- <form
name="f1" action = "authentication.php" onsubmit = "return
validation()" method = "POST">
-
<p>
-
<label>
UserName: </label>
-
<input
type = "text" id ="user" name = "user"
/>
-
</p>
-
<p>
-
<label>
Password: </label>
-
<input
type = "password" id ="pass" name = "pass"
/>
-
</p>
-
<p>
-
<input
type = "submit" id = "btn" value = "Login"
/>
-
</p>
-
</form>
- </div>
- // validation for empty
field
- <script>
-
function
validation()
-
{
-
var
id=document.f1.user.value;
-
var
ps=document.f1.pass.value;
-
if(id.length==""
&& ps.length=="") {
-
alert("User
Name and Password fields are empty");
-
return
false;
-
}
-
else
-
{
-
if(id.length=="")
{
-
alert("User
Name is empty");
-
return
false;
-
}
-
if
(ps.length=="") {
-
alert("Password
field is empty");
-
return
false;
-
}
-
}
-
}
-
</script>
- </body>
- </html>
Step 4: Creating a CSS File for Designing
- body{
- background: #eee;
- }
- #frm{
- border: solid gray
1px;
- width:25%;
- border-radius: 2px;
- margin: 120px auto;
- background: white;
- padding: 50px;
- }
- #btn{
- color: #fff;
- background: #337ab7;
- padding: 7px;
- margin-left: 70%;
- }
How to run the login form?
- To run the login form, open the xampp control panel and run the
apache server and PHP.
- Now, type localhost/xampp/folder name/file name in the browser
and press Enter key.
- All setup is done now. Enter the username and password in the
login form and click the login button.