How to display user name on header after login using PHP session

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:

Before Login display user name from session after login in php

After Login display logged-in user information in PHP 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.

  1. How to Develop a Login form with a Simple Menu in PHP and MySQL
  2. How to create a login and registration form with menu in PHP and MySQL

Watch YouTube Video

 get data from login page in phpAssumption

  1. You must have basic knowledge of PHP, HTML, CSS, JavaScript/jQuery. You can read below topics for more details:
    1. How to install xampp for Windows with setup for PHP development
    2. How to write PHP code with examples for beginners
    3. How to build a simple CRUD application in PHP and MySQL

to display users in phpFolders and Files

Below is a screenshot of folder structure and files I am using:

Display username on webpage after login in PHP

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
Folder 'js' - In this folder I have my custom JavaScripts

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:

Display Username After Login in PHP and Mysql

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:

display logged-in username in PHP

The table 'users' has 5 columns in it.

  1. id - it is the primary key and auto incremented
  2. email - email id of the user
  3. name - name of the user
  4. password - password for the user
  5. 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.

display name using session in php

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.

  1. server - in our case it is localhost
  2. userid - we are using root user
  3. password - no password for user root
  4. 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.

displaying profile image and username on header when user logged in using php

Let us look at the code for login.php


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 style.css in top_menu.php.


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.

Step 6 - Add JavaScript/jQuery (validation.js)

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.

login form in phpTest 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:

Display logged in user name using PHP Session

Now click on Login, you will see Login Page is displayed and it is highlighted in the menubar.

display logged in user information in PHP and mysql

In the login form, enter email id as '', password as '123', Click on Login button. You should be able to login successfully and see the home page.

PHP Display Username After Login

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:

user profile information display after login php

Hope you understood how the application works now.

Watch YouTube Video

download source code to display user name from session after login in phpDownload Source Code

I have put all codes in a zip file. You can download it by clicking on the Download button below. You do not need to register yourself to download it. You can directly use the code or you can modify them as per your requirements.

display user name from session after login in phpConclusion

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.