How can i secure my login page in php?

In this tutorial, I'll be teaching you how you can create your very own secure PHP login system. A login form is what your website's visitors can use to log in to your website to access restricted content, such as a profile page. We will leverage MySQL to retrieve account data from the database.

The Advanced package includes additional features and a download link to the source code.

1. Getting Started

There are a few steps we need to take before we create our secure login system. We need to set up our web server environment and ensure we have the required extensions enabled.

1.1. Requirements

  • If you haven't got a local web server set-up, I recommend you download and install XAMPP.
  • XAMPP is a cross-platform web server package that includes the essentials for back-end developers. It includes PHP, MySQL, Apache, phpMyAdmin, and more. It's not necessary to install all the software separately with XAMPP.

1.2. What You Will Learn in this Tutorial

  • Form Design — Design a login form with HTML5 and CSS3.
  • Prepared SQL Queries — How to properly prepare SQL queries to prevent SQL injection and therefore preventing your database from being exposed.
  • Basic Validation — Validating form data that is sent to the server using GET and POST requests [username, password, email, etc.].
  • Session Management — Initialize sessions and store retrieved database results. Sessions are saved on the server and are associated with a unique ID that is saved in the browser.

1.3. File Structure & Setup

We can now start our web server and create the files and directories we're going to use for our login system.

  • Open XAMPP Control Panel
  • Next to the Apache module click Start
  • Next to the MySQL module click Start
  • Navigate to XAMPP's installation directory [C:\xampp]
  • Open the htdocs directory
  • Create the following directories and files:

File Structure

\-- phplogin
  |-- index.html
  |-- style.css
  |-- authenticate.php
  |-- logout.php
  |-- home.php
  |-- profile.php

Each file will consist of the following:

  • index.html — The login form created with HTML5 and CSS3. We don't need to use PHP in this file. Therefore, we can save it as plain HTML.
  • style.css — The stylesheet [CSS3] for our secure login system.
  • authenticate.php — Authenticate users, connect to the database, validate form data, retrieve database results, and create new sessions.
  • logout.php — Destroy the logged-in sessions and redirect the user to the login page.
  • home.php — Basic home page for logged-in users.
  • profile.php — Retrieve the user's account details from our MySQL database and populate them with PHP and HTML.

2. Creating the Login Form Design

We will now create a form that our users can use to enter their details and submit them for processing. We will be using HTML and CSS for this part of the tutorial as PHP will not be necessary on this page.

Edit the index.html file with your favorite code editor and add the following code:



	
		
		Login
		
	
	
		

Login

If we navigate to the index page in our web browser, it will look like the following:

//localhost/phplogin/index.html

Profile Logout

Home Page

Welcome back, !

The above code is the template for our home page. On this page, the user will encounter a welcome message along with their name being displayed.

We need to add CSS for the home page. Add the following code to style.css file:

.navtop {
	background-color: #2f3947;
	height: 60px;
	width: 100%;
	border: 0;
}
.navtop div {
	display: flex;
	margin: 0 auto;
	width: 1000px;
	height: 100%;
}
.navtop div h2, .navtop div a {
	display: inline-flex;
	align-items: center;
}
.navtop div h2 {
	flex: 1;
	font-size: 24px;
	padding: 0;
	margin: 0;
	color: #eaebed;
	font-weight: normal;
}
.navtop div a {
	padding: 0 20px;
	text-decoration: none;
	color: #c1c4c8;
	font-weight: bold;
}
.navtop div a i {
	padding: 2px 8px 0 0;
}
.navtop div a:hover {
	color: #eaebed;
}
body.loggedin {
	background-color: #f3f4f7;
}
.content {
	width: 1000px;
	margin: 0 auto;
}
.content h2 {
	margin: 0;
	padding: 25px 0;
	font-size: 22px;
	border-bottom: 1px solid #e0e0e3;
	color: #4a536e;
}
.content > p, .content > div {
	box-shadow: 0 0 5px 0 rgba[0, 0, 0, 0.1];
	margin: 25px 0;
	padding: 25px;
	background-color: #fff;
}
.content > p table td, .content > div table td {
	padding: 5px;
}
.content > p table td:first-child, .content > div table td:first-child {
	font-weight: bold;
	color: #4a536e;
	padding-right: 15px;
}
.content > div p {
	padding: 5px;
	margin: 0 0 10px 0;
}

Now that we have our home page set-up, we can redirect our users from the authenticate.php file to our home page, edit authenticate.php and replace the following line of code:

echo 'Welcome ' . $_SESSION['name'] . '!';

With:

header['Location: home.php'];

If you log in with the test account, you should see something like this:

//localhost/phplogin/home.php

This is a pretty basic home page. You can customize it to how you want now that you understand how it works.

6. Creating the Profile Page

The profile page will display the account information for the logged-in user.

Edit the profile.php file and add the following code:

Chủ Đề