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 byAnonymous (not verified)on Sat, 06/29/2013 - 09:25

sir,your code is well and good...but i have one doubt..mytask is that to create the login page..database member can log in to my login page..after log in,the member who would logged in details will be shown..that member can edit that details..after finished edit process to press the finish button..again the loginpage will be open..after that member can logged out..please send the source code for above task..this is my mail id:[email protected]
Submitted byAnonymous (not verified)on Thu, 07/11/2013 - 17:29

ur code real help me thanx.....
Submitted byAnonymous (not verified)on Mon, 07/15/2013 - 03:15

its very nice
Submitted byalikargaran (not verified)on Sat, 07/27/2013 - 13:55

hi...thanks alot for your very useful code But, how can I show the user profile(fname,lname,...) in the home page after the validation?
Submitted byhanny (not verified)on Mon, 07/29/2013 - 11:04

GOD is so great of your life . It works! Keep up the good work and GOD BLESS :)

Submitted bySakthi Nagarajan (not verified)on Wed, 08/07/2013 - 13:05

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /mrdthome/webwalk/public_html/admin/login_exec.php:1) in /mrdthome/webwalk/public_html/admin/login_exec.php on line 3 Query failed
Submitted bySakthi Nagarajan (not verified)on Wed, 08/07/2013 - 16:46

In reply to by Anonymous (not verified)

I removed the white space and upload the files. Now i am getting the result of Query failed. What mistake i have done? Hence it works without errors on localhost.
Submitted byAnonymous (not verified)on Wed, 08/07/2013 - 16:55

In reply to by Sakthi Nagarajan (not verified)

Change the error_reporting in your php.ini to: error_reporting = E_ALL & ~E_NOTICE
Submitted byRashminda (not verified)on Wed, 08/21/2013 - 01:06

Im a biginer for web but I dne well hahah thnks lot
Submitted bykanzz (not verified)on Wed, 08/21/2013 - 16:40

cool, dude!!
Submitted bysowmya (not verified)on Wed, 08/28/2013 - 12:50

Fatal error: Call to undefined function get_magic_quotes_gpc() in C:\AppServ\www\myphp\login_exec.php on line 19
Submitted byHaraprasad Hota (not verified)on Sun, 09/29/2013 - 18:39

Page1:login.php
<?php
session_start();
mysql_connect("localhost","root","") or die("error connect");
mysql_select_db("task2") or die(" error database");
$email=$_POST['email'];
$pass=$_POST['password'];
$login=mysql_query("SELECT * FROM login WHERE email='$email' AND password='$pass'");
$sql=mysql_num_rows($login);
if($sql!=0){
$_SESSION['email']=$email;
header('Location: login_verify.php');
}
else {
echo "Wrong Username or Password";
}
?>
page2:login_check.
<?php
session_start();
if(isset($_SESSION['email'])){

echo "<center><h1>".$_SESSION['email']."Welcome to Home page</h1></center>";
}
else{
header('Location: login_im.php');
}
?>
<a href="newlogout.php">Logout</a>
page3:logout.php
<?php
session_start();
session_destroy();
header("location:/login_im.php");
?>

Submitted byCami (not verified)on Wed, 10/23/2013 - 20:21

excellent! thank you very much!
Submitted bylazlo freeman (not verified)on Thu, 10/24/2013 - 17:41

I'm new to this things of web designing I want to know how can a create a table in my database on-line, like the way is being done on a localhost.
Submitted byJOOBIN (not verified)on Fri, 11/01/2013 - 05:16

Unbelievable!!! I tried over and over many of Tutorials and every time i was not successful,but IT IS WORKING Thank you so much
Submitted byrahul agrahari (not verified)on Thu, 11/07/2013 - 18:51

how to connect VB.net to SQL Server2005 provide source code plz
Submitted byLito Saulo (not verified)on Wed, 11/13/2013 - 08:18

Hi, The login script works, but I'm not sure how the logout script should work. I was expecting a Logout button somewhere. Also, in the login script. 1) the password shows when it is typed in. Is there a way to just show ****** instead? 2)This code in the index.php shows as comment. Not sure why. 0 ) { echo '
    '; foreach($_SESSION['ERRMSG_ARR'] as $msg) { echo '
  • ',$msg,'
  • '; } echo '
'; unset($_SESSION['ERRMSG_ARR']); } ?> Otherwise, it's great.Thanks.
Submitted byjlbtechno (not verified)on Wed, 11/13/2013 - 16:42

The tutorial and codes above is really helpful to me. Thank you very much.
Submitted byNaser (not verified)on Wed, 12/04/2013 - 19:24

Your code is perfect! Thank you very much!
Submitted byjaypal (not verified)on Sun, 12/15/2013 - 13:31

how to add pdf file in database through php form at admin side
Submitted bydan123456 (not verified)on Mon, 12/16/2013 - 12:28

how if iwant to add selection..two text area and 1 selection... how to write for selecetion???hope someone can help me...
Submitted byArslan (not verified)on Fri, 12/20/2013 - 02:14

its very good tutorial .. i just copy and paste and its run .. thanks alot...
Submitted bybert (not verified)on Fri, 12/20/2013 - 17:00

can anyone help me or can show me codes on how to create a login. i have 2 tables in my database namely: "users" and "roles". a user should login by their corresponding roles. how can it be done? or is it possible that way? pls help me asap. Thank You.
Submitted byBert Rica (not verified)on Fri, 12/20/2013 - 17:28

your sourcecodes are very useful. i've got a project and it includes logging in. I have many types of admin: 1) admin that has a FULL ACCESS of the system, 2) admin that has a limited access only, and 3) admin that has a lesser access than the second one. Member users also has a limitted access to my information portal project. i have tables in my database namely: "users(user_id,user_name,password)", "users_data(data_id, lname, fname, mname, email)", "roles(role_id,role_type)". My Problem is that, how will it be when a USER will login, he will be redirected to its corresponding page(according to its role type). and so with the other users. please help me with this one. Please send me your sourcecode for this at [email protected]. thank you in advance!
Submitted byWILSON MUGENDI (not verified)on Mon, 01/06/2014 - 18:56

Thanks man,i like this,it worked well with me.
Submitted byBhushan Dod (not verified)on Thu, 01/30/2014 - 10:43

Hello Sir,
I'm also try to develop login system.I'm new in PHP.Please send me complete login system for my reference if possible.my email-id is:[email protected] .

Submitted bykames (not verified)on Tue, 02/25/2014 - 00:45

thank u very much............ really helped me lot....... thanks
Submitted bymanjul (not verified)on Wed, 02/26/2014 - 15:52

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\manjul\connectivity\students\simple_db\login_exec.php:4) in C:\xampp\htdocs\manjul\connectivity\students\simple_db\login_exec.php on line 44
Submitted bychisenga lupiya (not verified)on Sun, 03/09/2014 - 21:21

its working but its not given error when the password and username dont match it just logs them need help
Submitted byGoodPaddy (not verified)on Mon, 04/14/2014 - 17:54

how do you link this to your html page ?
Submitted byrock on (not verified)on Fri, 04/25/2014 - 11:47

how to change my password visibility to ********

Add new comment