How to create a login system in PHP – Learn PHP backend programming



How to create a login system in PHP – Learn PHP backend programming. Today we will learn how to easily create a login system from scratch using PHP.

At 18:30 and 24:00 I made a mistake in this video by accidentally combining Procedural PHP with Object Oriented PHP.
Please don’t turn off “annotations” so you can see the “fix” for what you need to write instead once I get to it in the video. Or see what needs to be changed here:

Change to: $result = mysqli_query($conn, $sql);

Change to: !$row = mysqli_fetch_assoc($result);

mmtuts is a YouTube channel that focuses on teaching beginner and advanced courses in various multimedia related skills.
We plan to make tutorials available on programming, video production, animation, graphic design, and on software such as the Adobe Creative Cloud programs.

PHP for beginners is a how to series that teaches the PHP coding language to people who are just starting out learning programming. The course teaches how PHP scripting can be made easy and teaches how to build many apps such as a login system, a comment section, how to upload images, how to create users in a website, and much more. Creating dynamic websites with PHP is easy and should not be seen as otherwise, which is why we want to explain the language in a easy to understand way for beginners.

If you have suggestions on new courses, or specific lessons within existing courses you would like to see, then feel welcome to submit them in the comment section or in a private message. ALL suggestions will be seen, but not all will be replied to since we get quite a few every day.


computer study says:

nice video…
I have a simple and short PHP tutorial in my channel “computer study”

Giovanni Zito says:

why did you not use PDO? This log in script is not very secure :)

Daniel See says:

like your videos , keep it up . helpful and crystal clear

Albert Pinto says:

can you provide a written procedure of this video plz?

MrZakrencony says:

i’m sorry to annoy you, how to prevent many people from logging on the same account?
unless you explain that in one of the further videos, then i’ll be sure to find it

MrZakrencony says:

will this work with many people loggin in?
is the session saved on the server side or client side? if server then how to clear a session only for one person, who’s actually clicking on the logout?

mark61196 says:

hi, at 24m, i replace
if(!$row = $result->fetch_assoc()){
if(!$row = mysqli_fetch_assoc($result)){
but it give me an error:”Parse error: syntax error, unexpected ” (T_STRING) in C:xampphtdocsclogin.php on line 10″

whats wrong?

< ?php session_start(); $conn = mysqli_connect("localhost", "root", "", "login"); $uid = $_POST['username']; $pwd = $_POST['password']; $sql = "SELECT * FROM utenti WHERE username='$uid' AND password='$pwd'"; $result = mysqli_query($conn, $sql); if(!$row = mysqli_fetch_assoc($result)){ header("Location: login.php"); }else{ $_SESSION['username'] = $row['username']; header("Location: index.php"); } ?>

Insane Techs says:


TechGuide says:

It works for me but it does not take me to the front page

Just4lbert Gaming says:

your SQL Database code didnt work in 000webhost, there is errror when I Press Go

arslan islam says:

thanks,its working

Kappa says:


ZouS says:

thx very much

IWRConsultancy says:

‘We won’t worry about code injection or hashing just now..’

Sorry, but that’s highly irresponsible. People are going to follow your instructions and have their site hacked.

Виктор Шалагин says:


Asad Fiaz says:

+mmtuts..Im using mysql database .My insertion query is not running,
Here is the code.Please fix it :(
< ?php include "dbh.php"; $first=$_POST['first']; $last=$_POST['last']; $uid=$_POST['uid']; $pwd=$_POST['pwd']; echo $first."
echo $last.”
echo $uid.”
echo $pwd.”

$sql_query=@”INSERT INTO user (first,last,uid,pwd)
VALUES (”,$first’,’$last’,’$uid’,’$pwd’)”;

header(“Location: login.php”)


mmtuts says:

Hey everyone! Please read this before commenting!

I have a updated version of this tutorial which I linked to in the video description and on the cards that pop up in the beginning of the video. If something is not working, or you have questions or complaints about the safety of this login system, then watch that video instead. THIS video is part 1 of a 4 part series, where the next couple of episodes teaches about SQL injection prevention, error handling, and styling of the login form. BUT LIKE I SAID, the updated video has ALL of that included in it, which is why it is a longer episode! :)

Thank you.

chooomedia says:

great Tutorial! Very informatic and easy-to-learn produced. A Cherry-Picking How-2 for PHP codeing :)

Abhijith TN says:

How to find user who ran DROP or DELETE statements on our SQL Server?

mcDoggyKiip says:

tnx a lot for this video, i just needed to know more about sessions

Shamsuddeen Omacy says:

Nice video! I like all your tutorials. Because, why no?

Jake evermore says:

Hey~ I really like your vids!
It’s just that I got a problem:

< ?php include 'dbh.php'; $first = $_POST['first']; $last = $_POST['last']; $uid = $_POST['uid']; $pwd = $_POST['pwd']; echo $first."
echo $last.”
echo $pwd.”
echo $uid.”

^When I try and ‘echo’ this out I get:

Notice: Undefined index: first in D:XampphtdocsSupermarktsignup.php on line 4

Notice: Undefined index: last in D:XampphtdocsSupermarktsignup.php on line 5

Notice: Undefined index: uid in D:XampphtdocsSupermarktsignup.php on line 6

Notice: Undefined index: pwd in D:XampphtdocsSupermarktsignup.php on line 7

Can someone please help me with this?

Dipesh Gyawali says:

in login.php my code isn’t running what’s the problem pls solve in

error occured:

Fatal error: Call to a member function fetch_assoc() on boolean

and the code is:

< ?php session_start(); include 'databasehandler.php'; $uid=$_POST['uid']; $password=$_POST['password']; $sql="SELECT * FROM logintest WHERE uid='$uid' AND password='$password'"; $result=$conn->query($sql);

if(!$row = $result->fetch_assoc()){
echo “Your username and password is incorrect”;


pls solve that issue thanks

Kev Habno says: – someone help??


well you have an sql injection now .

SilentRim says:

I’m stuck around the login form. It keeps throwing me “HTML Error 500”.

Aniruddha Koley says:

Make a easy file download system in php

Philcob Josol says:

This is what happens when I test the signup form. 15:40

< ?php include 'dbh.php'; $first = $_POST['first']; $last = $_POST['last']; $uid = $_POST['uid']; $pwd = $_POST['pwd']; echo $first."
echo $last.”
echo $uid.”
echo $pwd.”

Benjaminosa23 says:

You are my hero, thank you so much for these videos

blended says:

i watched millions of tutorials…
but this is something that worked.. :-O <3
subbed :-)
because… Why not?

Arun Krishna Chitturi says:

what is varchar datatype??
is it char data type??

Levente Kovács says:

Your data base handling is prone to sql injection, should have used a prepared statement or stg.

reza jafari says:

I really wanna to thumb up all of your videos , but it is off

Iwan Dobrev says:

How does this auto-fill work?

Dhruv Rathod says:

Loved it! I have never seen coding explained so easily. Thank you.

rashid maqbooljutt says:

sir you are great

Roberto Samuel says:

Thank you! Code single, fast and very good.

Thapelo Modise says:

gud tut…m a beginner to php n want to use pdo… so i wanna know how do i secure from sql injection using pdo?
I have subscribed tour channel n find it very helpful. u my mentor

Alexandre Afogan says:

Great, I liked it!

Ikhouvan MANDJES says:

a simpler way to look if your data has been send from the form the the other file is var_dump

Write a comment


Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.

Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!

Send this to a friend

▷ Other ReviewsVehicles▷ Show Cars▷ Motorbikes▷ Scooters▷ Bicycles▷ Rims & Tires▷ Luxury BoatsFashion▷ Sunglasses▷ Luxury Watches▷ Luxury Purses▷ Jeans Wear▷ High Heels▷ Kinis Swimwear▷ Perfumes▷ Jewellery▷ Cosmetics▷ Shaving Helpers▷ Fashion HatsFooding▷ Chef Club▷ Fooding Helpers▷ Coktails & LiquorsSports▷ Sport Shoes▷ Fitness & Detox▷ Golf Gear▷ Racquets▷ Hiking & Trek Gear▷ Diving Equipment▷ Ski Gear▷ Snowboards▷ Surf Boards▷ Rollers & SkatesEntertainment▷ DIY Guides▷ Zik Instruments▷ Published Books▷ Music Albums▷ Cine Movies▷ Trading Helpers▷ Make Money▷ Fishing Equipment▷ Paintball Supplies▷ Trading Card Games▷ Telescopes▷ Knives▷ VapesHigh Tech▷ Flat Screens▷ Tech Devices▷ Camera Lenses▷ Audio HiFi▷ Printers▷ USB Devices▷ PC Hardware▷ Network Gear▷ Cloud Servers▷ Software Helpers▷ Programmer Helpers▷ Mobile Apps▷ Hearing AidsHome▷ Home Furniture▷ Home Appliances▷ Tools Workshop▷ Beddings▷ Floor Layings▷ Barbecues▷ Aquarium Gear▷ Safe Boxes▷ Office Supplies▷ Security Locks▷ Cleaning ProductsKids▷ Baby Strollers▷ Child Car Seats▷ Remote ControlledTravel▷ Luggages & Bags▷ Airlines Seats▷ Hotel Rooms▷ Fun Trips▷ Cruise Ships▷ Mexico Tours