User registration with login is an essential component of a website. I will help you build a simple, light-weight user registration in PHP with login backed by MySQL database. Read on!
There are lots of PHP components for user registration available on the Internet. But these contain heavy stuff with lots of dependencies.
The code should be lightweight, secure, feature-packed and customizable. The example you are going to see now will have these attributes and will be sufficient for any website.
Example code for user registration in PHP
In this example, I have created user registration in PHP with the login script.
On the landing page, it shows a login form with a signup link. The registered user can enter their login details with the login form. Once done, he can get into the dashboard after successful authentication.
If the user does not have an account, then he can click the signup option to create a new account.
The user registration form requests username, email, password from the user. On submission, PHP code allows registration if the email does not already exist.
This example code has client-side validation for validating the entered user details. And also, it includes contains the server-side uniqueness test. The user email is the base to check uniqueness before adding the users to the MySQL database.
Create user registration and login form
I have created three HTML view login form, registration form and the dashboard for this code.
Below HMTL code is for displaying the login form to the user. In this form, it has two inputs to allow the user to enter their username and password.
Without these details, a validation code will not allow the login to proceed. The login form tag’s on-click attribute is with loginValidation(). This function contains the login form validation script.
On submitting this login form, the PHP code will validate the user. If the users clear the authentication, then it will redirect him to the dashboard.
If the user attempts to log in with the wrong data, then the code will display a login error message in the login form. If you want to limit the failed login attempts, the linked article has an example of that.
See also login form with forgot password and remember me for a complete suite of registration, login, forgot password and remember me options.
Following is a user registration form to get minimal user data from the user. All form fields are mandatory.
After validation, the PHP registration will take place with the posted form data.
Landing page loads login form page.
And the below CSS is for presenting this example of user registration.
Registration and login form validation
On invalid data submission, the code will return a boolean false. It forces the user to enter the required fields by highlighting them.
Process user registration in PHP
After submitting the form details, it processes user registration in the PHP code.
This code uses default form submit to post data to the PHP. If you want the user registration code with AJAX, then we have to prevent the default submit with a script.
I have added this code at the beginning of the user-registration-form.php. It checks if the user submitted the form. Then, it invokes the registerMember() method defined in the Member model.
I have shown the Member model class code below. It contains all the functions related to this user registration and login example.
In the registerMember() function, it checks if the posted email already exists. If so, it truncates the registration flow and returns the error. Otherwise, it creates the Insert query to add the member record into the MySQL database.
The loginMember() function checks if there is any match for the entered login details. If the match found, it clears the authentication and allows the user to access the dashboard.
PHP login authentication code
The code in the login-form.php file above invokes the authentication function after login.
This is the user dashboard code. It shows a welcome message with the logged-in member name. It also has an option to logout from the current session.
At the top of the home.php file, I have done the session based authorization check. This is to ensure that, users who do not have access to this page are redirected to login page.
If a not-logged-in user tries to access this page, the session will be cleared and he will be redirected to login page.
Following logout script is common code. You can use it any of your PHP projects. It clears the complete user session and redirects the user to the home / login page.
Following is a simple database wrapper for MySQL / mysqli. You can use it in any of your PHP and MySQL projects.
MySQL database script
Below SQL script shows the MySQL database table’s create statement. It also has the specification for the key and indexes.
mport this script into your PHP development root to execute this example.
Screenshot of user registration and login form
Login form with validation response:
User registration form server-side validation response: