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 byarkard00 (not verified)on Tue, 08/14/2012 - 00:35

Your code was very useful. Thank you. But, how can I show the username in the home page after the validation?
Submitted byAnonymous (not verified)on Fri, 10/19/2012 - 04:54

In reply to by arkard00 (not verified)

You can display the username by adding something like this: }else{ echo"Welcom " .$_POST['username']; } after fetching the row i.e $sql=mysql_num_rows($query); Thank you
Submitted byBlake Gillman (not verified)on Wed, 04/15/2015 - 02:14

In reply to by Anonymous (not verified)

Well I noticed that you guys didn't have this solved, so I did it myself. Just put this code above the HTML tag:
Submitted byMahesh A (not verified)on Sat, 11/03/2012 - 03:32

Thanks Argie. Your article has helped me a lot. Such a error-less code is what a fresher looks for. Well written n well explained. Thanks buddy.- Mahesh

Submitted byAnonymous (not verified)on Fri, 11/30/2012 - 22:32

Gosh, another beautiful tutorial. Appreciate the work. Thank you xxxx
Submitted byAnonymous (not verified)on Fri, 12/07/2012 - 10:44

thankyou verymuch..the only tutorial worked after trying tons of tutorials...really thankyou....
Submitted byAnonymous (not verified)on Tue, 12/11/2012 - 04:58

This tutorial seems really easy to follow but I get the following message when I copy and past into sql #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1.CREATE TABLE IF NOT EXISTS `member` (2. `mem_id` int(11) NOT NULL AUTO_INCREM' at line 1 I dont know what the problem is with the syntax for my version which is 5.0.91-log Can you help? sorry am a total newbie and this is probs really easy.
Submitted byAnonymous (not verified)on Fri, 02/15/2013 - 01:23

In reply to by Anonymous (not verified)

Take out the leading line numbers. Example: 1.CREATE ... should be CREATE ...
Submitted bygrateful (not verified)on Thu, 01/29/2015 - 10:31

In reply to by Anonymous (not verified)

yeah you right.... just delete all the leading line number (example. 1. ---, 2. ---, etc) "1. create ...." become just "create ..." delete the leading line number Thanks to Argie
Submitted byNoreen (not verified)on Thu, 10/16/2014 - 15:35

In reply to by Anonymous (not verified)

just change the 'member' to MEMBER and others..

Submitted byAnonymous (not verified)on Wed, 12/12/2012 - 14:44

hi thanks a lot. Ur code is very useful bt can you provide the validations to this form
Submitted byAnonymous (not verified)on Fri, 12/14/2012 - 15:12

realy useful source codes.
Submitted byAnonymous (not verified)on Tue, 12/18/2012 - 17:05

super thank you to the creator of this program... this really helps a lot...thank you!!!
Submitted byAnonymous (not verified)on Thu, 01/31/2013 - 18:33

You have made my day! thanks :) everything is working for me.
Submitted byAnonymous (not verified)on Fri, 02/15/2013 - 18:28

Your codes needs to be updated!
Submitted byAnonymous (not verified)on Thu, 02/21/2013 - 10:55

Big thanks for the registration and login scripts, i've just implemented both and they work beautifully (after initial confusion with double index pages heh) - definately the most simple/logical tutorial to follow on this topic ive seen
Submitted byAnonymous (not verified)on Fri, 02/22/2013 - 03:31

your code very useful ,but if i want to validate the registration form ,that is why a unique "username" will save or it already used echo "This UserName is already used. Please try another username" what can i do ????????????? please help me my E_mail - "[email protected]" I already use your code it is useful.thanks,
Submitted byAnonymous (not verified)on Mon, 03/04/2013 - 09:39

errors,errors errors..... "Warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent in /Applications/XAMPP/xamppfiles/htdocs/xampp/example/section2/sec1/login_exec.php on line 53 Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/xampp/example/section2/sec1/connection.php:2) in /Applications/XAMPP/xamppfiles/htdocs/xampp/example/section2/sec1/login_exec.php on line 59" help please!!!
Submitted byAnonymous (not verified)on Tue, 03/05/2013 - 19:01

what will be the input for the login form and how to incorporate the same in the backend?main issues are not there.disgusting.
Submitted byAnonymous (not verified)on Sat, 03/09/2013 - 12:37

thank you!! this tutorial helped me a lot :)
Submitted byAnonymous (not verified)on Mon, 03/11/2013 - 16:29

user name and password not found-- am getting this error after i login. i already edited the connection.php. below is my code.. thanks in advance.

$mysql_user = "my_user"; $mysql_password = "mypassword123"; $mysql_database = "mydatabase"; $mysql_hostname = "localhost"; $conn = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database"); mysql_select_db($mysql_database, $conn) or die("Could not select database");
Submitted byAnonymous (not verified)on Sat, 03/16/2013 - 14:39

Logged out. Clicked on back. No login page. Logging in directly.
Submitted byAnonymous (not verified)on Mon, 03/18/2013 - 10:39

easy to understand
Submitted byAnonymous (not verified)on Fri, 03/22/2013 - 20:29

This was really helpful but I have noticed that the password is not case sensitive which obviously limits security. I don't know if it is my mysql database settings or the actual login script that is the problem
Submitted byAnonymous (not verified)on Wed, 03/27/2013 - 16:50

good explore
Submitted byAnonymous (not verified)on Fri, 03/29/2013 - 06:13

how do i create user and passwords who will login into then system after the login page creation
Submitted byAnonymous (not verified)on Sat, 04/13/2013 - 15:49

how to create web services in login page php and mysql. please help me. thank u.
Submitted byAnonymous (not verified)on Sun, 05/19/2013 - 22:50

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampplite\htdocs\index.php:1) in C:\xampplite\htdocs\index.php on line 3 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampplite\htdocs\index.php:1) in C:\xampplite\htdocs\index.php on line 3
Submitted byvikasgenus (not verified)on Wed, 01/15/2014 - 01:02

In reply to by Anonymous (not verified)

i have also same Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent if you have any answer please help me. [email protected]
Submitted byAnonymous (not verified)on Tue, 05/28/2013 - 02:38

Thank you
Submitted byAnonymous (not verified)on Tue, 05/28/2013 - 06:10

thanks dude, it works
Submitted byAnonymous (not verified)on Fri, 05/31/2013 - 01:16

Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: NO) in C:\xampp\htdocs\BloodBank\connetion.php on line 6 Could not connect database Line 6 in connection... $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database"); mysql_select_db($mysql_database, $bd) or die("Could not select database"); Plz help me.
Submitted byalikargaran (not verified)on Sat, 07/27/2013 - 14:29

In reply to by Anonymous (not verified)

hi...first you should create your database and create your user in your database with password then in connection.php follow: if you have any question contact me with my email: [email protected]
Submitted bySakthi (not verified)on Wed, 08/07/2013 - 12:57

In reply to by alikargaran (not verified)

$mysql_hostname =""; what should come here
Submitted bySufiyan (not verified)on Sun, 12/25/2016 - 04:36

In reply to by alikargaran (not verified)

Can u plz explain how I can write username and password in mysql database where I can put username and password query in database
Submitted byAnonymous (not verified)on Sat, 06/01/2013 - 21:50

If you would like to obtain a good deal from this paragraph then you have to apply these techniques to your won web site.
Submitted byAnonymous (not verified)on Thu, 06/06/2013 - 13:43

There is just a small error. The query should be "SELECT COUNT(*) FROM tablename WHERE..." instead of whatever you wrote Argie. Except that, everything else is fine.
Submitted byAnonymous (not verified)on Thu, 06/06/2013 - 13:44

There is just a small missing detail. The query should be "SELECT COUNT(*) FROM tablename WHERE..." instead of whatever you wrote Argie. Except that, everything else is fine.
Submitted byAnonymous (not verified)on Mon, 06/10/2013 - 22:48

hello, i'm Ameh james...thanks for this tutorial. however when i tried it, on the web page after clicking the login button...i get this error message: "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\test\links\login.php on line 13 Sorry, there is no username John with the specified password.Try again" "John" is what i have in the database. please i need help
Submitted byAnonymous (not verified)on Tue, 06/18/2013 - 21:24

Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\test\REG1\login_exec.php:11) in D:\xampp\htdocs\test\REG1\login_exec.php on line 70
Submitted byAnonymous (not verified)on Fri, 06/28/2013 - 21:01

it is nice tutorial but where i found the username and password?

Add new comment