Display user name on header after login using PHP session and MySQL | Logged-in user information

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
friends PHP session is very useful to track users activities when user logs into an application today I'm going to show you how you can use PHP session to display the usern name on top of the screen after a successful login here you can see this is a very small application when the user logged in into this system it is displaying welcome test user which means user is logged in and it is displaying the username test user and there is a log out option so this shows that user is logged in into the system now if I click on log out you can see it is no longer showing the username or log out now it is showing login so it shows that user has logged out of the system if I click on login is login form is displayed and let me log in here so now again it is showing the username welcome test user log out but it's not showing login so using PHP session you can determine whether user has logged in or not and if user is logged in you can display the usern name and log out otherwise you display login so let me show you quickly how we can do this using PHP and MySQL I have created a folder called user under HD Doc and and within that user folder these are the minimum folders and files so far I have created so if I open this in Visual Studio code so CFG folder we have DBC connect. PHP so this just to connect to a demo database there is a top menu.php just created for the menu only so this is just an HTML file with a menu you can see boot step 5 is used here and there's a custom style used just for the menu only there's a font or some library and jQuery library is also added here if you see in the menu so this is very simple menu we are using home login and log out as menu items so if you run it now Local Host and then you have to give user then top menu.php so you can see it is displaying the menu home login and log out if I click any nothing is happening because we have not added any code yet so for the database let us just see we are using users table this is the table that we'll be using and we'll use email ID and password to login into the system I am inserting one row in this table just for testing purpose only so email ID is test test.com username name is test user and password is this this is just md5 hash of 1 2 3 so our password will be 1 2 3 so let me just create this table we'll use PHP my admin SQL window to create the table so the table is created and row is inserted in this table we'll just add a homepage here we create new file so we create an index.php just an H1 only and we included top menu.php so if I run it now so I'll just give Local Host user so it is showing the homepage now in the top menu we'll use index.php for homepage so we'll just add a little bit of CSS here so this is my homepage just to show you so now let us create a login form here we'll create a new file inut top menu.php first let us create the form so we'll create email ID and password as the fields and we'll have a submit button now we'll add a password field so we have created the form if you see top menu.php I need to close the body and the HTML now let us change the top menu here add login.php let me run it now click on login so it says login. PHP not found so let me just see you can see the login. PHP is created in CSS folder so let let us just move it outside now let us run it so it is showing the login form now let me just quickly add few CSS here so we'll add a class here we'll reduce the width of the form if we refresh it now okay so let us give some padding and margin this looks better let us reduce the width a little bit let us add a heading also go to login so let us now write PHP code when the form is submitted so here we'll take the values of email ID and password from the form and with those values we'll check with the database if the values are correct we'll let the user login otherwise we will give invalid user ID password password we have the submit Buton name is submit so we'll just check if form is submitted so we just create md5 hash of the password and then we'll check with the database if you see DBC connect. PHP our connection object is dollar Co NN so we are using it here for the prepare statement and then you bind the parameters here email ID and password so we are these are all string so we'll give SS so we are just executing the statement and checking whether the number of rows greater than zero if it is greater than zero then we are fetching the values in dollar row array and we'll just get the name and email in the session variable once we get the values in the session we'll just redirect use that to the homepage so we are redirecting the user uh to the homepage after successful login we just add an L statement here with error message now we have to add another else here if the statement does not get executed properly so here we just add another else all we have to do is just start the session here and also initialize the variables and dollar email and uh error message now you will use this email in our form so let us now update the top menu because we have the session variable available now so we'll display login and log out based on the session variable so let me just add code here if user is logged in we want to show the log out so let me just put it here you can see we are using dollar session name so if it is set that means user is logged in so you want to show the log out otherwise we will show login and we want to display the usern name also and now let us write the code for log out also because once we log in we need to log out so we's create a new file logout.php so we are starting the session then unsetting all the session variables deleting the session and then redirecting the user to the homepage so let us now refresh and see how it is working now click on login test at test.com password 1 2 3 so it looks like it's not working it's showing the login again it's not showing log out or user name so we have some problem here so let us just see uh we have used the session variable here that's looks fine so all we have to do here we have need to add the session start here now if you see in the login. pH we have already session start we have used and at the same time we have used top menu. P so you don't need this anymore so let us remove this and now let us try so refresh it we logged in and it is actually user is logged in so it is now showing log out and username so you click on log out so it is showing login now and we don't see username and log out anymore so you just need to add some CSS here just to display them in a better way so if we click on login again test at test.com so it is showing now log out and test so we'll just add some CSS here we need to show log out on the right side and username also on the right side so we'll just add add some classes here so let us just add a d here okay now it is fine so we'll display test user also on the right side we add the same class here this is the name so test user is displayed here we'll just let us just align it and give some color let us add a class called user here and we'll add that in our style. CSS so white color and give some padding here so let us change that to 18px and also we'll add welcome here before the user We'll add welcome so now you can see it is displaying welcome test user and log out so if I click on log out now it is no longer showing username and log out now it is showing login only if I click on login and login again so it is fine now log out okay so this is working fine so at the end I just want to show you an additional feature here we want to show the active menu item so if I select home home should be active if I select login login should be active we just want to add some jqu script here to show the active menu item in top menu.php we have ID home and for login we have ID login so we'll use this so we'll use this ID to add some jid script so let us go to index.php here we need to close the body and HTML so we'll just add active class here we already have the Styles added for this active class in style. CSS here it is so background color and color added for this active class so if we run it now see home is selected for login we have not added code so let us add code for login also we'll just copy it so if you refresh now so login is now selected so this way we can show active menu items which is selected you can use this PHP session in many different places to know if the user has logged in to the system or not you can visit the website codow to.com to download the source code you don't need to sign up and there are many other topics related to PHP development you can go through them if you are interested I have given all the related links in the video description to watch more videos please subscribe to the channel thank you
Info
Channel: Codehow2
Views: 6,192
Rating: undefined out of 5
Keywords: php, php development, php web development, php tutorial, php course, php developer, mysql, html, css, learn-php, php session, php login, login form, mysql prepare statement, display profile, login php, php logout, php login form, php mysql login, session in php, php session and cookie, php cookes, php login and register tutorial, navigation bar in html and css, admin dashboard bootstrap 5, bootstrap navigation bar, admin login page in php mysql
Id: 8qwmx_q1syc
Channel Id: undefined
Length: 16min 48sec (1008 seconds)
Published: Wed Nov 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.