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
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.
$member = mysqli_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['mem_id'];
$_SESSION['SESS_FIRST_NAME'] = $member['username'];
$_SESSION['SESS_LAST_NAME'] = $member['password'];
$errmsg_arr = 'user name and password not found';
$errflag = true;
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
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.
This page is the home, you can put some stuff here......
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