How to create login system in PHP?

Answered

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`(
 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
 `username` VARCHAR(30) NOT NULL,
 `email` VARCHAR(50) NOT NULL,
 `password` CHAR(128) NOT NULL
 )
 

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

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

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

 <?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";
 //header("location:loginsucess.html");
 }
 else
 {
 print "invalid username or password";
 }
 ?>
 

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

 <html>
 <body>
 <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">
 </form>
 </body>
 </html>
 

Step 5:
Create Registration Function (Register.php)

 <?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`)
 VALUES('$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.