When an user logs in successfully into a web application, we can use PHP
$_SESSION to store user name in a session variable and display on the header. You can get the user name from MySQL database using PHP login code and just check if session variable is set or not. If it is set, just display it. This is supposed to be very simple as below:
To understand it in more details, let us develop an application where user can login into a system and after successful login, user name will be displayed on top right corner. So, let us develop a simple menu, a login form and write logout PHP code for complete understanding.
A screenshot is given below to get an idea of how it will look like:
You can see after login, user name is displayed on top right corner along with Logout link.
You can also read below topics to get more details about Signup and Login.
- How to Develop a Login form with a Simple Menu in PHP and MySQL
- How to create a login and registration form with menu in PHP and MySQL
Folders and Files
Below is a screenshot of folder structure and files I am using:
I have created a folder called 'login' under 'htdocs' as I am using XAMPP. If you are using WAMP, create 'login' folder under 'www'.
Folder 'cfg' - In this folder I have kept
dbconnect.php which is used to connect to the MySQL database.
Folder 'css' - My custom stylesheet is in this folder
A menu bar(
top_menu.php) will be displayed on the top and there will be links for Home (
index.php) and Login (
login.php). Below is the screenshot of the login page:
Step 1 - Create a MySQL table to store user details
Let us create a table named 'users' in MySQL database. This table will have user details. I have a database called 'demo'. So 'users' Table will be created in this database. If you have an existing database other than demo you can also use it. Just make sure same table name does not exist already. Table structure is given below:
The table 'users' has 5 columns in it.
- id - it is the primary key and auto incremented
- email - email id of the user
- name - name of the user
- password - password for the user
- signup_date - date of signup, default is current date, this is not required for login, used as information purpose only
Create table script for this table is given below, you can use this code to create the table. You can also download and get the scripts from download section later in this topic.
Verify if data is inserted correctly, it should have below data in it, signup_date should be current date.
Step 2 - Connect to MySQL database (dbconnect.php)
Use below script to connect to the database. Note that we have this database connection php program in 'cfg' folder. This is written once and used in every program where database connection is needed. This will be easy for maintenance and also will enable reusability of the code.
I am using mysqli_connect() function which needs 4 parameters.
- server - in our case it is localhost
- userid - we are using root user
- password - no password for user root
- database name - demo in our case.
If connection is successful then it will return true and false otherwise. This Boolean value is returned and stored in the $conn variable. We will include this
dbconnect.php in other php programs so that we do not need to write it again in the program. For detail database connection understanding please read topic How to Connect to MySQL database in PHP using mysqli_connect.
Step 3 - Create a top menu (top_menu.php)
This is the top menu to show the Menu items - Home and Login on a menu bar. We will include this menu in all the other pages so that this menu will be visible in all pages.
You can see for Logout and Login, we must check if user is already logged in or not. If user is logged in then session variable 'name' will be set, in that case display Logout only. But, if user is not already logged in, display Login only. This session variable 'name' is set in
login.php discussed in next step. Now look at line 27 and 28, we are displaying user name if user is logged in.
Step 4 - Develop a Login form (login.php)
Our Login form will have email id as userid and password for the user to login to the system.
Let us look at the code for
After successful login, I am setting two session variables 'name' and 'userid' with the values of user name and email id.
A piece of jQuery code is added to add active class for this page just to highlight the Login link in the menubar.
Step 5 - Add CSS (style.css)
Let us add below styles. We have already added
You can see simple styles are added here. Keep this
style.css file in your css folder. Note that we added "active" class in our pages using jQuery, so, in
style.css, properties for "active" class are added.
You have already seen
togglePwd() function is called from login form when checkbox - "Show Password" is clicked. It is used to show/hide password. So, here is the code for it. Keep
validation.js file in js folder.
Now, I will add a simple Home page and code for logout. Remember that Home page is not a proper Home page, it is added just for navigation purpose only. See below code:
Lastly, I want to show
logout.php as below:
Here, I am deleting all session variables by using
session_unset() function. Then end the session using session_destroy() function.
Our development is complete, let us test the application now.
Test the Application
Make sure your XAMPP control panel is running with Apache and MySQL services are on. Open the browser and run localhost/login. You will see the home page displayed as below:
Now click on Login, you will see Login Page is displayed and it is highlighted in the menubar.
In the login form, enter email id as 'firstname.lastname@example.org', password as '123', Click on Login button. You should be able to login successfully and see the home page.
You should see user name is displayed along with Logout link. Now, click on Logout, you should now see Login in the menubar again, but Logout and user name are disappeared as shown below:
Hope you understood how the application works now.
In this example I have showed how you can display user name after successful login along with a simple menu. In some cases, you might have to display userid/email instead of user name. I made it very simple for you, you can always improve it. Hope it will be useful for you.