User Login

PHP Login Form with MySQL database and form validation

Login form – an entry point of a website to authenticate users. PHP login system requires users to register with the application first to log in later.

The registered users are managed in a database at the back end. On each login attempt via the PHP login form, it verifies the database to find a match.

It is a very simple and essential job. At the same time, it should be designed with security to guard the site. It should filter anonymous hits 100% not to let unregistered users get in.

Читайте также:  Java все для айпад

The PHP login form action stores the logged-in user in a session. It uses PHP $_SESSION one of its superglobals. It’s better to validate the existence of this session at the beginning of each page to be protected.

This PHP code can also be used to add an admin login for your control panel. Also, it can be used as a common authentication entry for both admin and user side of an application.

PHP login form code

This example is to design a PHP login form working with backend processing. The login page in PHP shows the UI elements like social login, forgot password and etc.

It posts data to process a username/password-based login authentication. This example uses the database to authenticate the user login.

This PHP login system is capable of linking the following code to the additional login form controls.

php login form

HTML form template

The landing page renders this template into the UI to let the user log in. It will happen when there is no logged-in session.

This form accepts the user’s login details username or email and a secure password. The submit event triggers the PHP login form validation and posts the login data to the PHP.

This PHP login form is responsive to the different viewport sizes. It uses simple CSS media queries for adding site responsiveness.

The form tag calls a JavaScript function validate() on the submit event. The below code includes the PHP login form validation script at the end.

        
Login
?>
Forgot password?

New user?

Signup Now

May also signup with

PHP login form action

A PHP endpoint script that is an action target of the login form handles the login data.

This login page in PHP sanitizes the data before processing them. It uses PHP filter_var function to sanitize the user entered authentication details.

It conducts the authentication process after receiving the user credentials.

This program puts the authenticated user details in a session. Then, it acknowledges the user accordingly.

processLogin($username, $password); if (! $isLoggedIn) < $_SESSION["errorMessage"] = "Invalid Credentials"; >header("Location: ./index.php"); exit(); > 

PHP login authentication model class

It contains the processLogin() function to check the PHP login form data with the database. It uses PHP password_verify() function to validate the user-entered password. This PHP function compares the password with the hashed password on the database.

The getMemberById() function reads the member result by member id. After successful login, it is called from the case to display the dashboard. It returns the array of data to be displayed on the dashboard.

ds = new DataSource(); > function getMemberById($memberId) < $query = "select * FROM registered_users WHERE $paramType = "i"; $paramArray = array($memberId); $memberResult = $this->ds->select($query, $paramType, $paramArray); return $memberResult; > public function processLogin($username, $password) < $query = "select * FROM registered_users WHERE user_name = ? OR email = ?"; $paramType = "ss"; $paramArray = array($username, $username); $memberResult = $this->ds->select($query, $paramType, $paramArray); if(!empty($memberResult)) < $hashedPassword = $memberResult[0]["password"]; if (password_verify($password, $hashedPassword)) < $_SESSION["userId"] = $memberResult[0]["id"]; return true; >> return false; > > 

After successful login, the site says there exists a session of the logged-in user. It can be shown in different ways.

In most sites, the site header displays the logged-in user’s profile link. It can be a clickable avatar that slides down a profile menu.

This PHP login system redirects the user to a dashboard page after login. This dashboard page shows a welcome message, about-user with an avatar.

The landing page checks the PHP session if any user has already login. If so, it will redirect to this dashboard page.

getMemberById($_SESSION["userId"]); if(!empty($memberResult[0]["display_name"])) < $displayName = ucwords($memberResult[0]["display_name"]); >else < $displayName = $memberResult[0]["user_name"]; >> ?>   body < font-family: Arial; color: #333; font-size: 0.95em; >.dashboard < background: #d2edd5; margin: 15px auto; line-height: 1.8em; color: #333; border-radius: 4px; padding: 30px; max-width: 400px; border: #c8e0cb 1px solid; text-align: center; >a.logout-button < color: #09f; >.profile-photo  

Welcome !

You have successfully logged in!

Click to Logout

PHP Logged in User Dashboard

Logging out from the site

This is a general routine to log out from the site. The following script clears the PHP session. Then it redirects back to login page in PHP.

Sometimes, the logout case may clear cookies. Example: In the case of using a cookie-based Remember Me feature in login.

Files structure

See the below image that shows the file structure of this simple PHP login form example. It contains a featured login form UI with the application view files.

The login action calls the PHP model on the submit event. It performs backend authentication with the database.

php login form files

Database script

Look at this SQL script which contains the CREATE statement and sample row data.

By importing this SQL, it creates database requisites in your development environment.

The sample data helps to try a login that returns success response on the authentication.

Test data: username: kate_91 password: admin123

-- -- Database: `blog_eg` -- -- -------------------------------------------------------- -- -- Table structure for table `registered_users` -- CREATE TABLE `registered_users` ( `id` int(8) NOT NULL, `user_name` varchar(255) NOT NULL, `display_name` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `photo` text DEFAULT NULL, `about` text DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- -- Dumping data for table `registered_users` -- INSERT INTO `registered_users` (`id`, `user_name`, `display_name`, `password`, `email`, `photo`, `about`) VALUES (1, 'kate_91', 'Kate Winslet', '$2y$10$LVISX0lCiIsQU1vUX/dAGunHTRhXmpcpiuU7G7.1lbnvhPSg7exmW', 'kate@wince.com', 'images/photo.jpeg', 'Web developer'); -- -- Indexes for dumped tables -- -- -- Indexes for table `registered_users` -- ALTER TABLE `registered_users` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `registered_users` -- ALTER TABLE `registered_users` MODIFY `id` int(8) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; 

Secure DataSource using MySQL with prepared statements

This DataSource is a common file to be used in any stand-alone PHP application. It uses MySQLi with prepared statement to execute database queries. It works with PHP 8 and 7+

This class is available in the project download zip file below. It is a common wrapper for MySQL using PreparedStatement.

Conclusion

We have seen a simple example on the PHP login form. Hope this will be useful to have a featured, responsive login form.

The article interlinks the constellations of a login form. It will be helpful to integrate more features with the existing login template.

Let me know your feedback on the comments section if you need any improvements on this PHP login system.

Источник

12+ PHP Login Form Templates to Download

Many modern legal websites have a built-in login page where users are directed to and have to input their credentials to be able to enter the backend. These sign-in pages are necessary for a site’s data connection and directory. If you’re designing a design website in which you’d like to incorporate a similar system, we’d say to take a look at PHP website in HTML5 with login details.

free php login form templates to download

These are equipped with editing and adding options so you’d face no hassles while designing the login page form actions for your site. A login page can also contain directions for users who forgot passwords. Feel free to check out login form templates for Laravel, jQuery, and More.

Website Pop-up for Login Template

free website pop up for login template

Free PHP Login Form with Sessions & Source Code

php login form with sessions

Form builder helps you build great login and registration forms for websites using simple codes and lines for a variety of sample form types, easily transport and use forms for various sites due to portability, and put in things like authentication, session, and other minutes for user benefit.

Login Form in PHP and Mysql Free Download

login form in php and mysql free download

Discuss Desk Login Simple Form Templates provide you with great options like category-based login forms, intuitive templates and customizable forms for your software/ website, and editable options when it comes to designing the forms for the login page. You can even get the source code and smart code snippets for implementing the best ideas in your login forms.

Free Responsive Bootstrap Login Form Template

free responsive bootstrap login form template

Free Responsive Bootstrap login basic forms templates are packed up in a handy .zip file and the forms are made up in the database. You can make use of the login form builder templates in personal and marketable projects, but you can’t be able to sell them or distribute them unswervingly.

Free Secure PHP Login / Signin and Registration System

secure php login and registration system

Secure PHP login and Registration systems are built up with HTML5 Bootstrap and PHP. The projects are made up of PHP. The admission forms that are created online use the Secure PHP Login and Registration System.

Create a Login Form in PHP

There are several model templates available for users on the internet. To create a login form printable in PHP first you have to create a table. Now, insert the data in the username table and create connectivity. Take the input from the user as the user’s ID and Password to get signed up.

How to Create Login Page in Dreamweaver

Your PDF website application can consist of a page that allows users to register and log in to the site. A login page consists of a database table of the registered users, an HTML form to make users enter the username and their password. Then you need to create a database table card of the registered users and you have to use PHP code for the login page in Dreamweaver. Now you have to add up the HTML form to make the users login into Dreamweaver. After the login, there is a verification of the username as well as password.

User Login Form with Forgot Password Option Template

user login page design template

The sample user login form template is easily accessible on the internet but with validation of certain languages. You can use your design perception to apply bits and portions from forms in docs you aim towards.

Free Admin PHP Login Page Design Template

admin php login page design template

If you’re designing up a page construction website then take a look at PHP admin login page design templates. These are prepared with editing and adding-up options so that you’d face no irritations while designing.

Free PHP Website with AJAX Login and Register Form

php ajax login and register form

You can create the Login and Registration Form with the help of PHP AJAX. The email and password of the user field are handled using PHP AJAX to accomplish the login process. You may also like PHP Registration Form Template.

Free Simple PHP User Login Script Website Template

simple php user login script website template

With the help of Simple PHP User Login Script Template, you can shield your site content. The PHP login script is free of cost and is easy to use.

Free Modern PHP Signup Login Form Template

modern php signup login form

phoeniixx.com

Free PHP Login Form with Database Connection

php login form with database connection

assets.ctfassets.net

Free PHP Authentication Directory Action Login Form

php directory action login form

idahohde.org

How to Create/Design Login Page in PHP with Sessions

The session variables are used up to store specific client’s data on the webserver for future use, as a web server does not know which of the client’s requests to reply to because the HTTP address does not uphold state. To create sessions in the PHP through the Login application form you have to first start a PHP session. Then you have to store the values in a PHP session variable. Now, read out the values of the PHP session variable. To destroy all sessions and unset a PHP session you can see the examples available on the internet. you may also see PHP wordpress website Templates & Themes. This login form professional template you see here is category-based. It is intuitive and packed with an array of customizable forms which you can upload for your portal. Full editing liberties are allowed on these templates so you can personalize the login page as you wish – including registration or log-in fields of your choice.

Источник

Оцените статью