How to Create Login Page in PHP/MySQL

Related code: How to Create Secure Login Page in PHP/MySQL. Yesterday I posted a tutorial on how to create a registration page using PHP/MySQL. To make some follow up with my registration page tutorial, I decided to create another tutorial on how to create a login page using PHP/MySQL. The Features of my login page contain input validation using PHP session. One unique feature also with my login page is the logout function, it is unique because unlike with other login page once the user click the logout button and click the back button of the browser, their system allows the user to go back as if it still logged in, which is not appropriate for security reason. But here in my login page once the user click the logout button, the user can no longer go back to their previous page. The only way to go back to the previous page is to login again. To start this tutorial let’s follow some steps below.

Creating Our Database

First we are going to create our database which stores our data. To create a database: 1. Open phpmyadmin 2. Click create table and name it. 3. Then name the database as "simple_login". 4. After creating a database name, click the SQL and paste the following code. CREATE TABLE IF NOT EXISTS `member` ( `mem_id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(30) NOT NULL, `password` varchar(30) NOT NULL, `fname` varchar(30) NOT NULL, `lname` varchar(30) NOT NULL, `address` varchar(100) NOT NULL, `contact` varchar(30) NOT NULL, `picture` varchar(100) NOT NULL, `gender` varchar(10) NOT NULL, PRIMARY KEY (`mem_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Creating Our Form

Next step is to create a form and save it as index.php. To create a form, open your HTML code editor and paste the code below in the upper part of the document or above the html tag. The code below is use to disallow the user to go back as if it still logged in. Paste the code bellow after the body tag of the HTML document
0 ) { echo '
    '; foreach($_SESSION['ERRMSG_ARR'] as $msg) { echo '
  • ',$msg,'
  • '; } echo '
'; unset($_SESSION['ERRMSG_ARR']); } ?>
Username
Password

Creating our Connection

Next step is to create a database connection and save it as "connection.php". This file is used to connect our form to database.

Writing Our Login Script

Next step is to create our login script that validates our input data and save it as login_exec.php. 0) { //Login Successful session_regenerate_id(); $member = mysqli_fetch_assoc($result); $_SESSION['SESS_MEMBER_ID'] = $member['mem_id']; $_SESSION['SESS_FIRST_NAME'] = $member['username']; $_SESSION['SESS_LAST_NAME'] = $member['password']; session_write_close(); header("location: home.php"); exit(); }else { //Login failed $errmsg_arr[] = 'user name and password not found'; $errflag = true; if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; session_write_close(); header("location: index.php"); exit(); } } }else { die("Query failed"); } ?>

Creating Our Authentication File

Next step is to create our authentication file and save it as auth.php. this code is use to disallow the user to go back as if it still logged in.

Creating Our Home page

Next step is to create a homepage and save it as home.php. Untitled Document

Login successfully

This page is the home, you can put some stuff here......

logout

That’s all you have already created your login page with unique features. Hope this code will help you, see you guys in my next tutorial. We've created a more secure registration and login page using CodeIgniter and Ion Auth. We highly recommend you use this instead if you want to secure your site. However, we're using CodeIgniter framework on this. If you are interested, kindly visit Secure Registration and Login Script with PHP and MySQL using CodeIgniter and Ion Auth.

Comments

Submitted byMania_c (not verified)on Sat, 05/03/2014 - 02:38

In reply to by rock on (not verified)

Just use "<input name="password" type="password">"

Submitted bySorin (not verified)on Tue, 05/06/2014 - 20:30

In reply to by rock on (not verified)

You can use an iframe.

<iframe src="index.php"></iframe>

Submitted bysaj (not verified)on Fri, 05/16/2014 - 20:23

i got an error message. please help me to clarify Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\an\connection.php:19) in C:\AppServ\www\an\login_exec.php on line 68
Submitted bynasir5 (not verified)on Sat, 05/17/2014 - 03:18

It give error that database could not be selected... please tell which database is selected
Submitted byhangonon Sun, 05/18/2014 - 01:58

ur codeis superb help in log out code
Submitted byThanks for your code (not verified)on Mon, 06/02/2014 - 15:39

i want to update my data..after updating when i click button automatically it moves to logout page..i dont know what to do..i checked everything..in localhost it works perfectly but after upload into server it create problem

Attach the source code in zip and upload it for us to download please
Submitted byMeena S (not verified)on Thu, 07/10/2014 - 17:24

this code is ver useful
Submitted byAyman Y. Dasa (not verified)on Fri, 08/08/2014 - 06:05

I think it should be like
  1. $_SESSION['SESS_FIRST_NAME'] = $member['fname'];
  2. $_SESSION['SESS_LAST_NAME'] = $member['lname'];
Submitted byTrino Tarizulu (not verified)on Fri, 10/17/2014 - 14:18

Nice codes. It worked for me :)
Submitted byRanjeet Kumar … (not verified)on Mon, 10/20/2014 - 00:53

I am getting output query failed .plz tell me where i am wrong
Submitted bygayathri Selvam (not verified)on Fri, 10/24/2014 - 16:24

thanks for this code ....it fully running
Submitted byVab (not verified)on Mon, 10/27/2014 - 08:26

this really helped me, thanks.
Submitted byAnoop0118 (not verified)on Thu, 12/04/2014 - 13:01

Hello Thank You So Much For Detailed Explanation
Submitted byFeretzen (not verified)on Mon, 03/09/2015 - 21:26

Hi! I installed and everything is working smoothly. Thank you very much for sharing such a great tool and explanation. Regards, Fernando
Submitted bybaraka (not verified)on Sat, 04/25/2015 - 20:26

Thanks Man! Be blessed
Submitted byamirhossein (not verified)on Mon, 05/11/2015 - 03:56

hi dear i just wanna to say thank you ..i was desperately wanted this information god bless you bye amirhossein
Submitted byilyn (not verified)on Wed, 05/13/2015 - 22:20

can you please help in next previous, last and first buttons in php/mysql
Submitted byXnameR (not verified)on Wed, 05/13/2015 - 23:57

Nice tutorial ... but missing a register page can someone help me to make it. or someone is make reg page for this tutorial :D Thanks for sharing.
Submitted byAvery T. Horton, Jr. (not verified)on Sat, 05/23/2015 - 02:15

$_SESSION['SESS_MEMBER_ID'] = $member['mem_id']; 56. $_SESSION['SESS_FIRST_NAME'] = $member['username']; 57. $_SESSION['SESS_LAST_NAME'] = $member['password']; 58. session_write_close(); Names don't match!!
Submitted byJoe Don (not verified)on Thu, 06/04/2015 - 23:13

I will try this soon. Please, I will need similar in PHP on online students examinations in five subjects(computer science, chemistry, biology and physics) with instant result at the end of exams.
Submitted bylygi (not verified)on Sat, 06/27/2015 - 13:19

i want a feed back form and its comments and the name of the person who comments are shows in bellow the space

Submitted byHassnain Mehmood (not verified)on Fri, 08/07/2015 - 22:17

true but this code is too lenthy and difficult
Submitted byl0new0lf99 (not verified)on Fri, 09/25/2015 - 20:08

this is by far the best sample login Ive used, thanks for sharing

hi
i am riham
how to hide password in password text box????
pls help me

Submitted byKashif Ejaz (not verified)on Thu, 12/10/2015 - 02:51

Argie I have a different issue...I made login,index,home,connection,auth.php and transfer it to server folder, this folder contain website files, also In server I created database paste the sql code.......but how it will run? your help/answer will be highly appreciated, because i am a beginner, plz urgently, [email protected]
Submitted byKashif Ejaz (not verified)on Thu, 12/10/2015 - 03:09

Argie I have ceated th page it asking username and password, but dont have [email protected], its giving message " could not connect data base" what is the solution?
Submitted byMishek (not verified)on Sun, 02/21/2016 - 18:20

once I log user out he can relog in by using back arrow navigation.when I include the hath.PHP in my home.PHP nothing is happening when user login details are inserted
Submitted byjohn lito (not verified)on Fri, 09/23/2016 - 12:08

I want to create registration form n from tat username n password mentioned in registration I want to login.. so please help me out in this as quick as paossible............ very useful source code
Submitted byJoseph Zachwa (not verified)on Wed, 12/07/2016 - 08:10

I created it well but the password it doesn't hide. How can I do it so that I can hide it?
Submitted byjust testing it out (not verified)on Sun, 05/14/2017 - 19:47

Thanks for that, I have been looking around the net and most places want you to signup just to see the code. This works great. If anyone is having issues, all you need to do is follow each step.
Submitted byvenice (not verified)on Fri, 10/27/2017 - 14:45

hi, good afternoon po, medyo may napansin lang po ako, panu po ba gawin ung pag ibinack mo sya ay dapat wala ng makikitand username at password?
Submitted bymehdi youseffi (not verified)on Sun, 12/10/2017 - 01:54

your code very good for me. Thank you.
Submitted bypriyanka khawale (not verified)on Sat, 02/24/2018 - 15:24

please send the php code for ...nursery management system...on this email-> [email protected]
Submitted byPirien (not verified)on Sun, 04/29/2018 - 18:47

Great stuff, i cannot wait to trial it out

Add new comment