Home PHP Login Registration System with PHP Data Object (PDO)

Login Registration System with PHP Data Object (PDO)

-

PHP Login Registration System with PDO Connection using SHA-256 Cryptographic Hash Algorithm to store Password

Download Complete Source Code

Things you should know before following this tutorial:

  1. PHP
  2. MySQL
  3. Bootstrap (optional)

If you’re a PHP beginner then yes this tutorial is for you as well, just follow the steps carefully.

Here we going to develop a quick login registration system with PHP and PDO extension, by end of this tutorial you should be able to use PDO connection and be able to develop your own login and registration feature and include in your project.

Quick Note on Password Encryption:
SHA-256 Cryptographic Hash Algorithmis a one way cryptographic function it can not be decrypted back, so keep in mind hash is not a encryption. if you want more secure methods to store password I would suggest to use bcrypt or scrypt. if your using PHP 5.5 version you can easily use new in built php functions password_hash() and password_verify().

Let’s begin:

Step 1: Create Database and required tables:

Create Database or use existing and add following table:

User stable structure

Step 3: Database Configuration file:

The basic need for every dynamic application is Database, let’s add new file called database.php this file is having PDO database connection script to use. you can add following script to the file, just don’t forgot to change connection variable as per your server setting.

database.php

Step 4: Design Login and Register Form:

I have added both the forms on index.php as this is just a demo, you can easily move them to the proper location for example you can create separate files for both the pages.

Also, for design we are using bootstrap here.

login form:

Login Form

User registration form:

Registration Form

Here is complete Code for index.php:

Note: Focus on PHP code from top section of index.php page below,  I have started php code by starting the session and the using the Database connection file we were created in Step 3, after that I have Application library file which having Class and function I will explain it in the next step.

Step 5: Application Library file:

We are using library.php file in index.php folder, library file having few functions to handle request coming from index.php page, let’s have look on page below:

A quick details on the application lib page functions:

Register() – as name suggest this function is to create/add new user to the database

isUsername() – used to check user name availability validation.

isEmail() – used to check email address availability validation.

Login() – used to get user login to the system by using username/email and password.

UserDetails() – to get the user details according to the specified ID.

Note: This is a better practice to use centralize application library if your working on core PHP, it helps to track functionality and gives us code re-usability and mainly important thing is it helps us to mange our code effectively.



Step 6: Profile Page:

I am using a demo profile page to redirect user after successfully login or user registration to say hello along with the logout button.

profile.php:

Profile Page

Step 7: Logout Page:

logout.php:

All done, try to run your code if you following the steps or you can easily download complete source code use bellow link to download. you can also test live demo to get more understandings. let me know if you get any issues with this tutorial by using comment box below.

Download Complete Source Code

You can also read about how to create Login Registration system using mysqli extension: PHP Login Registration – State Management

Enjoy this tutorial? Get iTech Empires updates delivered straight to your inbox.

No Spam, ever. We'll never share your email address and you can opt out at any time.

Yogesh Kolihttp://www.itechempires.com
Yogesh Koli is a software engineer & Blogger lives in India. He's driven by an addiction to learning and a love for adventure. he has 6+ years of experience working with the front-end and back-end web application development.

19 COMMENTS

avatar
13 Comment threads
4 Thread replies
2 Followers
 
Most reacted comment
Hottest comment thread
14 Comment authors
Yogesh KoliErnestmuhammadbinusmanShabir BalochFaRo Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
Ernest
Guest
Ernest

Hi there!

i have just purchased a login and register script using PDO, am geting an error when running..
can you help?

muhammadbinusman
Guest
muhammadbinusman

how to run this in phpmyadmin?

Shabir Baloch
Guest
Shabir Baloch

I want to learn PHP so can you help me my project title is book management system.
I am basically from Balochistan i am doing may BSCS form Balochistan University(UOB Quetta)

FaRo
Guest
FaRo

Hi, thank you for your tutorial. I have an error for DB() … it says it is undefined function. what should I do?

Patrick
Guest
Patrick

I have an error on this login please help me!

this is my error: Fatal error: Uncaught Error: Call to undefined function DB() in C:\xampp\htdocs\login\lib\library.php:67 Stack trace: #0 C:\xampp\htdocs\login\index.php(54): DemoLib->isEmail(‘pobsina@gmail.c…’) #1 {main} thrown in C:\xampp\htdocs\login\lib\library.php on line 67

Donald
Guest
Donald

Why don’t you require or include database.php on the index.php file? With the current way your script is written, it’s impossible for the register / login to work.

salichuma
Guest
salichuma

Hi Thank you for your tutorial, I get a problem with $db=DB(); it says undefined. I have separated user registration into another file and library in another file. Need your assistance if possible.

Ben
Guest
Ben

Hello,

I would like to know how to secure my site against XSS attacks.

Konstantinos Rizos
Guest
Konstantinos Rizos

Very nice tutorial but how i can do the same exactly with sqlite PDO????

Murdoc Lewis
Guest
Murdoc Lewis

Hi, how would I go about redirecting the user to their profile page with “php?id=” in the address bar? Also, I’m curious if there is a way to share this link with other users?

Thapelo
Guest
Thapelo

Thank you for your tutorial. It was very helpful.

omerida
Guest
omerida

Besides the use of sha256 instead of PHP’s built in password functions there are a couple of problematic samples in this article that I wouldn’t want novices to pick up as good habits. First, using constants for your database credentials is an unneeded global dependency for your DB() function. They really should be passed into DB as parameters. Consequently, DB() should be injected as a dependency to the DemoLib class, not pulled in from the global scope via $db = DB(). Second, password should be uniquely salted not just hashed. Again, there’s a reason PHP added password_hash, just use it.… Read more »

Yogesh Koli
Guest

I appreciate your feedback, above all the things is going to cover in upcoming tutorials.

omerida
Guest
omerida

Great! Although for #3 – you should never rely solely on clientside validation. You’ll still need to validate input with PHP (clients can misbehave or someone could do a POST to your form with using your HTML). For #5 – yes PDO with prepared statements guards you from SQL Injection attacks but if you’re storing what the user suppleis and then echo’ing without escaping, that’s a XSS vector.

OnceBuilder CMS
Guest
OnceBuilder CMS

Weldone! Add it do OnceBuilder

Yogesh Koli
Guest

Thank you, we will think about it.

Tutorials You May Like!

- Advertisement -