How to create login system in PHP?


How to create login system in PHP? I want to create the very basic login page in PHP.

Faiza_s Asked on November 5, 2017 in Programming.
Add Comment
1 Answer(s)
Best answer

In this article i am going to create a  login system in PHP. We use HTML, Css, PHP and MySql.

First of all you need a web server like xampp or wamp

We use xampp in this tutorial.

Step 1:
Let’s start with the database.
You need to create database using your localDB (MySQL)

In your browser type URL http://localhost/phpmyadmin then you saw the page like this:

Then Press Database button and create new database
After creating database create table “login” and set fields
ID, Username, Password

 CREATE TABLE `login`(
 `username` VARCHAR(30) NOT NULL,
 `email` VARCHAR(50) NOT NULL,
 `password` CHAR(128) NOT NULL

Step 2:
Create HTML form for Login (Login.html)

 <form method=post action=login.php>
 Username:<input type="text" name="username">
 Password: <input type="password" name="password">
 <input type=submit value="submit">

Step 3:
Create PHP Login Function (Login.php)

$db = mysqli_connect('localhost','root','','login')
 or die('cannot connect db');
$username = $_POST["username"];
 $password = $_POST["password"];
$sql = "SELECT * FROM users WHERE username= '$username' and password = '$password'";
$results= $db-> query($sql) or die (mysqli_error($db));
$count = mysqli_num_rows($results);
if ($count == 1)
 print "you have sucessfully login";
 print "invalid username or password";

Step 4:
Create HTML form for Registration (Register.html)

 <form action="register.php" method="post">
 Username: <input type="text" name="username"> <br>
 Password: <input type="password" name="password"> <br>
 Firstname: <input type="text" name="firstname"> <br>
 Lastname: <input type="text" name="lastname"> <br>
 Phone: <input type="text" name="phone"> <br>
 <input type="submit" name="submit" value="submit">

Step 5:
Create Registration Function (Register.php)

$db = mysqli_connect('localhost','root','','login')
 or die ('cannot connect to db');
$username = $_POST['username'];
 $password = $_POST['password'];
 $firstname = $_POST['firstname'];
 $lastname = $_POST['lastname'];
 $phone = $_POST['phone'];
$sql = "INSERT INTO users(`username`,`password`,`firstname`,`lastname`,`phone`)
$db->query($sql) or die(mysqli_error($db));

Black_Worant Answered on November 8, 2017.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.