PHP Simple Login and Registration Script
PHP Simple Login and Registration Script:
What is PHP Login and Registration Script?
This is a simple login and registration script based on PHP & MySQL. This script uses PHP session to store user details across multiple PHP pages to verify if the user is logged in or not. This script has minimal HTML/PHP/MySQL code which is easy to understand.
How to run Login-Registration Script:
You can simply download this script as a zip file, extract and run on your machine.
Here are the quick steps to run this script:
- Download this script into a separate folder
- Import database.sql file in a database ‘register_login_db’
- Now run this folder’s index.php file. You will find ‘login’ & ‘registration’ link on this page.
- Register some users and try to login with them.
- You will be able to access page-1.php and page-2.php content only after login.
- You can logout/login many times to test it.
Understanding code in each file
PHP Login and Registration script includes HTML Forms, PHP Session code & interacts with MySQL database to access users data. You will find that most of the code is well managed in a separate file. Here is the list of files along with their purpose.
- database.sql: This file contains a query to create table structure for the login-register script. You can either import this file or directly execute given queries in PHP-Myadmin or available database management tool.
- register.php: This file have an HTML registration form where user can enter his name, email and password. If registration data is valid, it will give success, else it will throw error.
- login.php: This file contains HTML login form where user can enter email and password. The Same page keep PHP code to check if given email/password is matched or not. If login is successful, it will redirect user to page-1 content.
- db-config.php: This file contains PHP code to create a connection with MySQL database and table. Login & Registration page includes this file to interact with MySQL data.
- page-1.php & page-2.php: These files contains sample content which are only accessible after login. If you try to access these pages without login, it will redirect you to login screen.
- logout.php: This file is used to distroy user data in PHP Session. This means that user will be logged out and redirected to login page.
How PHP Login and Registration code works
Mostly PHP scripts use PHP session or cookies to remember user details for login. This script also uses PHP session to track user login across multiple PHP pages to verify if the user is logged in or not.
Here is the basic idea behind PHP login and registration functionality.
- Firsly, registration form allow users insert user details in database.
- Then, user login using his registered email and password. Now if email and password found in database, user login considered successful. After successful login, we will save user email in PHP session variable.
- Now on each content page, we will check if user email is available in session variable or not. If available, user can view content pages else he will be redirected to login page.
- If a user clicks on logout link, PHP session gets destroyed and user needs to login again to view content pages.
- This is how Registration/Login works to protect certain pages/features only for authorized/logged-in users.
Common Mistakes while running Login and Registration Script
- You need to download and extracted all files preferably in separate folder.
- Make sure you have created database properly with the same as mentioned in db-config.php
- When running login-registration script, make sure folder name is right. Sometimes there is folder inside folders. Adjust your path or folder accordingly.
- Understand the overall data flow using comments in each file.
- If you found any issue with the script, please send your error screenshot at our facebook page. We will try to assist you if our schedule permits.
This PHP Login & Registration code is also available in Gitlab: https://gitlab.com/tutorialsclass/php-simple-login-registration-script