Login System in PHP and MySQL, Complete Registration System With Session: Everyday on the internet we see too many login forms. In other words, user registration system. Most of the website’s registration system built with PHP and MySQL like Facebook, WordPress etc.
Today I will show you how to make MySQL and PHP login system. In fact, I’ll give you the source code. Follow me step by step for creating a secure user registration system with PHP and MySQL. This system has a “session” feature. In other words, it is a secure login PHP system.
Login System in PHP and MySQL, Complete Registration System With Session Step By Step Guide:
You Have to Create These, Step by Step Guide:
- Database and Insert Table
- Registration Form
- Login Form
- Connect to Database
- Authentication
- Index page
- Dashboard Page
- Logout
- CSS File for Styling
Special Note: I gave Database, files, classes names from my own choice. you can change these names in your case.
Create a Database and Table
First, you have to create a database. So, Login in your Cpanel or localhost then goes to phpmyadmin. For create database goto to database section and type database name “register” and click on create.
After creating database successfully, Now create Database table, You can create table manually If you confuse about that. No Problem, Click on your Database named “register” then go “Query ” section paste this code and click on submit query.
SQL Query Code:
1 2 3 4 5 6 7 8 |
CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `trn_date` datetime NOT NULL, PRIMARY KEY (`id`) ); |
Connect To Database
Create a file named “db.php” and paste these codes below. This function is for Connect database to this program.
db.php
1 2 3 4 5 6 7 8 |
<!--PHP login System by WEBDEVTRICK (https://webdevtrick.com) --> <?php $con = mysqli_connect("localhost","root","","register"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } ?> |
Create Authentication File for Secure
Create a file named “auth.php” and paste codes below. This file creates session, for security and store cookies.
auth.php
1 2 3 4 5 6 7 |
<!--PHP login System by WEBDEVTRICK (https://webdevtrick.com) --> <?php session_start(); if(!isset($_SESSION["username"])){ header("Location: login.php"); exit(); } ?> |
Create Registration Page
Now you have to create a registration form. Basically, this form built-in HTML & CSS and functions are in PHP.
Create a Php file named “registration.php” and paste these codes and save it.
registration.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<!DOCTYPE html> <!--PHP login System by WEBDEVTRICK (https://webdevtrick.com) --> <html> <head> <meta charset="utf-8"> <title>Registration | Webdevtrick.com</title> <link rel="stylesheet" href="style.css" /> </head> <body> <?php require('db.php'); if (isset($_REQUEST['username'])){ $username = stripslashes($_REQUEST['username']); $username = mysqli_real_escape_string($con,$username); $email = stripslashes($_REQUEST['email']); $email = mysqli_real_escape_string($con,$email); $password = stripslashes($_REQUEST['password']); $password = mysqli_real_escape_string($con,$password); $trn_date = date("Y-m-d H:i:s"); $query = "INSERT into `users` (username, password, email, trn_date) VALUES ('$username', '".md5($password)."', '$email', '$trn_date')"; $result = mysqli_query($con,$query); if($result){ echo "<div class='form'> <h3>You are registered successfully.</h3> <br/>Click here to <a href='login.php'>Login</a></div>"; } }else{ ?> <form class="login" action="" method="post"> <h1 class="login-title">Register | Webdevtrick.com</h1> <input type="text" class="login-input" name="username" placeholder="Username" required /> <input type="text" class="login-input" name="email" placeholder="Email Adress"> <input type="password" class="login-input" name="password" placeholder="Password"> <input type="submit" name="submit" value="Register" class="login-button"> <p class="login-lost">Already Registered? <a href="login.php">Login Here</a></p> </form> <?php } ?> </body> </html> |
Create Login Page
Now create a file and save it with named “login.php” and paste the following codes given below.
login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<!DOCTYPE html> <!--PHP login System by WEBDEVTRICK (https://webdevtrick.com) --> <html> <head> <meta charset="utf-8"> <title>Login | Webdevtrick.com</title> <link rel="stylesheet" href="style.css" /> </head> <body> <?php require('db.php'); session_start(); if (isset($_POST['username'])){ $username = stripslashes($_REQUEST['username']); $username = mysqli_real_escape_string($con,$username); $password = stripslashes($_REQUEST['password']); $password = mysqli_real_escape_string($con,$password); $query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'"; $result = mysqli_query($con,$query) or die(mysql_error()); $rows = mysqli_num_rows($result); if($rows==1){ $_SESSION['username'] = $username; header("Location: index.php"); }else{ echo "<div class='form'> <h3>Username/password is incorrect.</h3> <br/>Click here to <a href='login.php'>Login</a></div>"; } }else{ ?> <form class="login" action="" method="post" name="login"> <h1 class="login-title">Login | Webdevtrick.com</h1> <input type="text" class="login-input" name="username" placeholder="Username" autofocus> <input type="password" class="login-input" name="password" placeholder="Password"> <input type="submit" value="Login" name="submit" class="login-button"> <p class="login-lost">New Here? <a href="registration.php">Register</a></p> </form> <?php } ?> </body> </html> |
Create an Index Page
What after login? of-course index page. So, create a page named “index.php” and pastes these codes give below.
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!--PHP login System by WEBDEVTRICK (https://webdevtrick.com) --> <?php include("auth.php"); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Welcome Webdevtrick.com User</title> <link rel="stylesheet" href="style.css" /> </head> <body> <div class="form"> <h1 >Welcome <?php echo $_SESSION['username']; ?>!</h1> <p >This is your secured index.</p> <p><a href="dashboard.php">Your Dashboard</a></p> <a href="logout.php">Logout</a> </div> </body> </html> |
Now Create a Dashboard Page
After created index page now you have create dashboard page. Basically, This is Profile Section. Now Create a file named “dashboard.php” and paste these codes.
dashboard.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!--PHP login System by WEBDEVTRICK (https://webdevtrick.com) --> <?php require('db.php'); include("auth.php"); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Dashboard - Secured Page</title> <link rel="stylesheet" href="style.css" /> </head> <body> <div class="form"> <p>Dashboard</p> <p>This is another secured page.</p> <p><a href="index.php">Index</a></p> <a href="logout.php">Logout</a> </div> </body> </html> |
Create Logout File
Basically, this file is to destroy session and redirect to index page. So, create a file named “logut.php” and paste these codes.
logout.php
1 2 3 4 5 6 7 8 |
<!--PHP login System by WEBDEVTRICK (https://webdevtrick.com) --> <?php session_start(); if(session_destroy()) { header("Location: login.php"); } ?> |
Now Finally Give Style with CSS On This login PHP system
The last thing create a CSS file to give style this program. Without CSS this program is only a frame. Create a file named style.css and paste these codes.
style.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
body { background: #2d343d; } .login { margin: 20px auto; width: 300px; padding: 30px 25px; background: white; border: 1px solid #c4c4c4; } h1.login-title { margin: -28px -25px 25px; padding: 15px 25px; line-height: 30px; font-size: 25px; font-weight: 300; color: #ADADAD; text-align:center; background: #f7f7f7; } .login-input { width: 285px; height: 50px; margin-bottom: 25px; padding-left:10px; font-size: 15px; background: #fff; border: 1px solid #ccc; border-radius: 4px; } .login-input:focus { border-color:#6e8095; outline: none; } .login-button { width: 100%; height: 50px; padding: 0; font-size: 20px; color: #fff; text-align: center; background: #f0776c; border: 0; border-radius: 5px; cursor: pointer; outline:0; } .login-lost { text-align:center; margin-bottom:0px; } .login-lost a { color:#666; text-decoration:none; font-size:13px; } .form{ text-align: center; color: white; } .form a { text-decoration: none; color: red; } .form a:hover { font-size: 20px; color: aqua; } |
Now This PHP login program is done. Hope you guys enjoyed this article and this source code and tutorial will helpful for you. Thanks for Visiting my blog.
I was recommended this blog by means of my
cousin. I am no longer positive whether this publish is written via
him as nobody else realize such specified about my problem.
You are wonderful! Thank you!
thanks for you comment, this is lot for me
Where the user data get store in localhost
In the mysql database. Go localhost/phpmyadmin
Nah postingan kali ini sangat bermanfaat, saya sering berkunjung dan membaca postingan disini.
Emang keren-keren kontennya. Salah satu blog yang recommended deh.
Terima kasih banyak. terus kunjungi blog ini untuk pembaruan lebih lanjut. sekarang Anda adalah bagian dari keluarga blog ini.
Hi
hi!,I like your writing so a lot! share we keep in touch extra about your
article on AOL? I need an expert on this space to solve my problem.
Maybe that’s you! Taking a look forward to look you.
infonya menarik.
Terima kasih
Terus berkarya ya, tulisannya enak di baca.
Bagus dan bermanfaat. Admin bales dong, please hehe.
terima kasih atas pujian anda
Hello my family member! I wish to say that this article is amazing, nice written and include approximately all important infos.
I would like to look more posts like this .
thank you very much
Nah postingan kali ini sangat bermanfaat, saya sering berkunjung dan membaca postingan disini.
Emang keren-keren kontennya. Salah satu blog yang
recommended deh.
Terima kasih banyak
Nah postingan kali ini sangat bermanfaat, saya sering berkunjung dan membaca postingan disini.
Emang keren-keren kontennya. Salah satu blog yang recommended
deh.
terima kasih banyak atas pujian ini
Whoa! This blog looks just like my old one! It’s on a totally different subject but it has pretty much the same layout
and design. Excellent choice of colors!
thanks, I think you are also creative like me
Baru sempet baca postingan ini hari ini, dan ternyata kontennya
menarik banget. Kayaknya aku akan sering mampir di mari,
semangat update terus kontennya ya min.
This excellent website truly has all of the info I wanted concerning this subject and didn’t
know who to ask.
thank you
Emang mantap kontennya. Semangat min untuk sering-sering
update. saya diam-diam baca dan share tiap kontenmu lho.
Terima kasih banyak
Min, saya mau tanya gimana sih caranya bisa konsisten dan punya konten bagus kayak gini.
Pertama-tama, terima kasih. Anda bisa saya juga pemula dalam blogging
you can speak indonesian ? or just translate it ?
google translate
Woah! I’m really digging the template/theme of this website.
It’s simple, yet effective. A lot of times it’s
hard to get that “perfect balance” between superb usability and visual appearance.
I must say you’ve done a awesome job with this. Also, the blog loads super quick for me on Internet explorer.
Outstanding Blog!
thank you very much.
Thanks for every other informative website. The place else
may just I am getting that type of info written in such a perfect
manner? I’ve a undertaking that I am simply now working on, and
I have been at the glance out for such info.
thank you
Thanks for the good writeup. It if truth be told was once a amusement
account it. Glance advanced to far introduced agreeable from you!
By the way, how can we be in contact?
Thanks you. You can contact me on facebook – https://www.facebook.com/w3shaan
Google
Wonderful story, reckoned we could combine some unrelated data, nevertheless truly worth taking a look, whoa did one particular find out about Mid East has got far more problerms too.
Thank You, Stay Connected.
Ahaa, its nice discussion concerning this article here at this web site, I have read all that, so now me also
commenting at this place.
Thank You, Stay Connected.
Hello sir IAM created a sql login database but if not telling a which forlder are created in sql database
Like: htdocs and another folder we created a xammp servers
Hey Pratik, sorry I did understand. Please message me on facebook, then I will resolve your problem. Thank you. FB account link – https://www.facebook.com/w3shaan
Hi there! I could have sworn I’ve visited this web site before but after looking
at many of the articles I realized it’s new to me. Regardless,
I’m definitely pleased I came across it and I’ll be book-marking it and checking back
frequently!
Thank you very much, Stay connected!
Outstanding post however I was wanting to know
if you could write a litte more on this subject?
I’d be very grateful if you could elaborate a little bit
further. Thank you!
Thank you very much, stay connected. I will in the future surely.
Awesome tutorial, thanks for sharing… love your site!!
Thank you very much, stay connected.
Hi, i think that i noticed you visited my blog thus i got here to go back the choose?.I’m attempting to
find issues to enhance my website!I suppose its ok to use
some of your concepts!!
Hello! Someone in my Myspace group shared this site
with us so I came to look it over. I’m definitely enjoying the
information. I’m bookmarking and will be tweeting this to my followers!
Fantastic blog and terrific style and design.
Thank you very much. Stay connected.
Hi there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!
Thank you very much, Stay connected.
Good day! Would you mind if I share your blog with my
zynga group? There’s a lot of folks that I think would really appreciate your content.
Please let me know. Thanks
I get pleasure from, result in I discovered just what I used
to be having a look for. You’ve ended my 4 day long hunt!
God Bless you man. Have a great day. Bye
Thank you very much, stay connected.
Hmm it looks like your website ate my first comment
(it was extremely long) so I guess I’ll
just sum it up what I submitted and say, I’m thoroughly enjoying your blog.
I as well am an aspiring blog writer but I’m still new to everything.
Do you have any suggestions for inexperienced blog writers?
I’d certainly appreciate it.
Google
One of our visitors not too long ago suggested the following website.
Nice blog here! Also your site loads up fast! What host are you using?
Can I get your affiliate link to your host?
I wish my website loaded up as fast as yours lol
I have not any affiliate account, get hosting from any well know hosting provider and optimize your site.
Amazing! Its actually awesome article, I have got much clear idea on the topic of from
this article.
Thank you very much, Stay connected.
I loved as much as you will receive carried out right here.
The sketch is tasteful, your authored subject matter stylish.
nonetheless, you command get got an impatience over that you wish be delivering the
following. unwell unquestionably come more formerly again since exactly the same nearly a
lot often inside case you shield this increase.
I’m truly enjoying the design and layout of your website.
It’s a very easy on the eyes which makes it much more pleasant
for me to come here and visit more often. Did you hire out a developer to create your
theme? Excellent work!
No, this is a free theme called “ionmag” you can download it.
Every weekend i used to pay a quick visit this website, because i wish for enjoyment, as this this web site conations genuinely nice funny data too.
5 6 7 8 Failed to connect to MySQL: No such file or directory
it work perfectly on localhost but not working on live server pls help
What problem are you facing? tell me
how to run this code? i’ve already followed the given instructions. thanks
You to upload files on the server or you can create local server inviorment by install Xampp
Hello, the code works fine, it can create users in the db table. But when I try to login with one of user accounts, the page comes blank. Help please!
Great blog 🙂 usefull tips and scripts. though i am haveing problems with getting this login code to work. could you please help me out?
lol i can see an OLD mysql there replace
$result = mysqli_query($con,$query) or die(mysql_error());
to this
$result = mysqli_query($con,$query) or die(mysqli_error());
DONT MESS UP MYSQL WITH mysqli
The login form is going to the next page Even if the password is incorrect why??
After Changing the action page to none,when click on submit on the login in page , it’s not going to the next page
Hi sir 🙂 I have spend a lot of time to find this… It looks everything is vorking but here is s mall thing… can u tell me why i can’t see the passwords in my db? it just generating a random password. by the way thank you for this work… i spend too much time on searching for right source…
sorry for my grammar
thnqq so much for this amazing……but there seems a small prblm when i use registration page. My registration page is successfully executed but the data is not being stored in data base….though i get user successfully registered….your help would be appriciated.
thanking you in advance
Right away I am ready to doo my breakfast, when having my breakfast coming again to read other
news.
Hi,
When I use the login.php, and it get validated, it does not go automatically to the index.php. The page is blank!
If manually enter index.php it show I am sign in.
Can you help to resolve the issue.
Thank you
Thank you
where i create a file of db.php while im using laravel
Why we have to include db.php in dashboard file but not in index file?
Everything works well except redirecting to index.php. It shows blank page after login. I hope many of them asking about the same issue. It would be helpful if you could reply. Thank You!
Good blog you have here.. It’s difficult to find excellent
writing like yours these days. I truly appreciate individuals like you!
Take care!!
hey bro u can do it nice work but u can make a PHP beginner course and javascript??
MD5 is insecure password encryption!
You should use hashing mechannism like bcrypt
Critical validations are misssing
I am curious to find out whаt blog syѕtem you’re utilizing?
I’m experiencing somе smalⅼ security issues witһ
mу latеst website ɑnd I’ɗ likе to find sοmething more safe.
Do you hаvе anyy solutions?
[…] Login System in PHP and MySQL Complete Registration System […]
[…] 9. Login System in PHP and MySQL | Complete Registration … […]
after updating doesnt work with php8
[…] 18. Login System in PHP and MySQL | Complete Registration System […]
[…] 7. Login System in PHP and MySQL | Complete Registration … […]
[…] https://webdevtrick.com/login-system-php-mysql/ […]
[…] 12. Login System in PHP and MySQL | Complete Registration … […]
[…] Login System in PHP and MySQL – webdevtrick.com […]
[…] 9. Login System in PHP and MySQL | Complete Registration … […]
[…] 9. Login System in PHP and MySQL | Complete Registration … […]
[…] This system has a “session” feature. In other words, it is a secure login PHP system. Login System in PHP and MySQL, Complete Registration System With Session Step By Step Guide: You Have to Create These, Step by … Visit site […]
[…] Login System in PHP and MySQL | Complete Registration System […]
[…] » Visit Now Mar 08, 2019 · Login System in PHP and MySQL, Complete Registration System With Session: Everyday on the internet we see too many login forms.In other words, user registration system. Most of the website’s registration system built with PHP and MySQL like Facebook, WordPress etc.. Today I will show you how to make MySQL and PHP login system.In fact, I’ll give you the source code.Author: Shaan […]
[…] Login System in PHP and MySQL | Complete Registration System […]
[…] Login System in PHP and MySQL | Complete Registration System […]
[…] Official Site: https://webdevtrick.com/login-system-php-mysql/ […]