Table of Contents
PHP State Management
State Management is the basic need for any dynamic application, here I am going to explain How to manage sessions in PHP using a simple register and login application which is going to help on to get better understandings.
Let’s get started:
Setup Database tables
First step is to setup a database table to store user’s login details, so go ahead and create following table.
CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `first_name` varchar(50) NOT NULL, `last_name` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, PRIMARY KEY (`id`) );
Create a new index.php page in a root directory of the project and add the following code. If you notice the code, we are just having a link to `login` and `registration` page with the little information about the project as this just our basic demo project.
I am going to consider you have your basic setup ready with the database and `users` table along with the index.php page, let’s move on to the next step.
`register.php` page is to have a registration feature, so that user can create their account for login and use the system, go ahead and use following code after creating your page.
If you look at the code from the above page, we have PHP as well html code.
So, we are using PHP to check if user already login to the system or not, if it is, then redirect the user to the profile page.
Keep in mind if you need to use sessions variables throughout the PHP page, we need to start the session at the top of the page using `session_start();` function, to be able to get or set the session variable(s) value.
Look at the following code, here we are validating each field from our registration page and sending the information to the server using AJAX, (I have added AJAX here instead of form submit feature, I just wanted to demonstrate the extra step so that you can lean AJAX as well, feel free to ask question about it in the comment box below.)
As we see we need PHP page called `register_user.php` for AJAX call to register new user so let’s create our required page to complete ajax call and to be able to register user.
Register the user is basically means to add new record into the database, so what we are doing actually is:
- Getting the Request from User Interface
- Adding it to the database table
- Responding with the message.
Whenever we need to interact with MySQL, you are going to need database connection script, so the better practice is to keep connection code in separate file and use it whenever required.
Sounds good right?
Yes, it does.
It’s better you keep this practice while working with Core PHP Project, get back to the tutorial so here we are doing same thing go ahead and create `db_connection.php` file and add following code.
Don’t forgot to replace the connection variables values as per your system setup.
What is Login?
If you’re new in the development field, you might think what does Login to the system means?
so, login is actually to enter into the system, the login page is the entry gate of the application with the Lock, and the User has to remember the key that is Password which is generated while creating the new account on registration page.
Simple process, right? if not I am going to make it simpler for you.
So, Let’s provide our users to be able to do the same, go ahead and use following code to create your Login page.
`Did you checked above code or just copied? `
So here is the thing why I am asking this question is because, I want to make sure you get each point of my tutorial.
what we do in the backend while processing the login request? here is the answer:
- Ask user for Login details
- Validate them by submitting to the server (match with existing or available records from the database)
- If match found: Fetch User basis details like ID, Name and store it to the session variable and redirect the user to the profile page.
- If the details are invalid: Get back to the login with the error message.
Move on to the next step.
After getting successful login we need to welcome greetings for the demo, you can use it into your existing application and display the profiles as per your requirements.
Logout user by destroying the PHP sessions:
Here is the folder structure for this tutorial:
You can download complete source code:
If you find this tutorial useful please share and let me know your feedback in comment box below:
You also learn to create login registration system using PDO – Login Registration System with PHP Data Object (PDO)