PHP MySQL Script to Build Session Based Login & Register System Using Bootstrap 4 in Browser
Dec 21, 2025
Buy the full source code of application here:
https://procodestore.com/index.php/product/build-a-image-gallery-crud-app-in-php-mysql-database-using-fancybox-library-in-javascript/
Show More Show Less View Video Transcript
0:08
Uh hello friends, today in this tutorial
0:10
we will be building a session based
0:11
login system inside PHP and MySQL. So
0:15
session as you all know it's a basically
0:17
it's a technique to store user
0:18
information inside the browser. So it
0:21
will be a login and registration system
0:22
guys. We will be doing it inside PHP and
0:25
MySQL database. So for this you need to
0:28
install this Apache control panel which
0:31
is exam control panel. So simply just
0:33
type here exam control panel and simply
0:36
download this software inside your
0:38
system. This is a cross-platform
0:40
software. It is available for Windows,
0:42
Mac and Linux. So it is available in all
0:45
the machines. So simply download this
0:47
software. After you install this
0:49
software guys, you just need to go to
0:51
your uh basically the C drive and go to
0:54
the exam directory and simply go to the
0:57
HT docs directory. And here you need to
1:00
make a folder here which will be called
1:02
as make directory which is login system.
1:07
CD into this
1:10
and open this inside your
1:13
Visual Studio Code text editor. And
1:15
basically guys just uh allow this and uh
1:18
here you just need to first of all make
1:20
a header dot php file guys
1:24
and index dot php file as well. So
1:26
inside header dot php guys we will be
1:29
including the bootstrap for our
1:31
application. So for building this we
1:32
will also be using some bootstrap. So
1:35
simply go to Google and simply include
1:37
the bootstrap CDN. So just type here
1:40
bootstrap CDN and simply go to
1:43
w3schools.com and simply include the CDN
1:46
for bootstrap.
1:48
I have written a complete blog post on
1:50
my tutorial website guys. So this is the
1:52
blog post that I have written here. So
1:54
if you want to follow along with it, you
1:56
can get the full source code
2:00
here. So it is given in the live chat.
2:04
Basically I have pinned this message in
2:06
the live chat so you can get the source
2:08
code. So right here guys basically
2:11
inside uh this blog post the very first
2:14
thing we will do is that we will write
2:16
the header dot PHP file. So header dot
2:19
PHP file is very easy guys. Let me show
2:22
you in what we will be doing inside this
2:25
file. So inside header dot php guys
2:28
basically just you will need a simple
2:31
title that is uh session based uh login
2:36
and register system
2:40
in PHP and MySQL.
2:45
So after this guys we also need to
2:47
include the bootstrap CDN. So simply
2:50
copy this bootstrap CDN after the title
2:52
you need to paste this CDN. So paste the
2:55
CDN here. So that's it. After that guys,
2:59
we also need the font awesome icons CDN.
3:02
So simply after this also paste the font
3:04
awesome CDN like this. So after this
3:08
guys, we will target our body which is
3:11
uh
3:13
here we will be targeting the body. So
3:15
we need to give some styling to it to
3:17
this body element which is uh these are
3:20
all bootstrap classes. So we will be
3:22
giving a background color which will be
3:24
of uh BG. BG stands for background and
3:28
we will change the background color to
3:30
secondary and uh the text will be in the
3:33
dark color. So these are all bootstrap
3:35
classes
3:37
and also guys we will be giving a style
3:39
attribute which will be equal to uh d-bs
3:46
-bg and opacity will be equal to
3:52
uh.5.
3:54
So 0.5 that's it.
3:57
So this is your header.php files guys.
4:00
So now inside our index dot php file we
4:03
need to include this file. So we can
4:05
simply include this file right here. We
4:07
can say include
4:09
once header dot php. So just include
4:13
this file guys like this.
4:17
So if you try to open this uh file uh
4:20
inside the browser guys which is now
4:23
localhost/lo
4:25
system p.
4:27
So now you can see the color will be
4:29
changed here. The background color is
4:31
successfully changed and also the text
4:33
color will also be changed. So these are
4:34
all coming through Bootstrap.
4:36
So this uh header dot PHP file is over
4:41
guys. So after this we need to create
4:44
this index.php after we include this
4:46
header.php. Uh let me just copy this
4:49
code here right here.
4:52
So first of all hit the like button guys
4:54
subscribe the channel as well. So
4:56
basically after that you include this
4:58
file guys we will have a H2 tag.
5:00
Basically we will be giving here the
5:03
heading which is login system with PHP
5:05
session.
5:10
So we will be giving a class to it of uh
5:17
text center
5:20
and margin bottom to three
5:23
margin from the top. So if you refresh
5:26
it you you will now see a heading here
5:28
login system with PHP session
5:31
and then we will have a simple
5:33
horizontal line basically.
5:38
So after this guys we will have a
5:40
container class of bootstrap.
5:47
So just re rename this to index html
5:50
guys so that I can get the formatting
5:52
correctly. Then later on I will rename
5:54
it to index.php. So as you can see here
5:59
just rename inside this container class
6:02
guys we will have a row class
6:05
and then we will have a column class and
6:09
then we will have a card class and also
6:11
it will be in the center position
6:14
div card header.
6:17
H3 we will have a card title class and
6:20
this will be saying as login. So there
6:22
will be a simple login here.
6:26
After this is complete guys, we will
6:28
have a card body class. Inside this we
6:32
will have a href tag. Basically this
6:35
will go to login.php.
6:38
And here we will have a simple paragraph
6:41
and
6:43
basically here we will be giving a
6:46
paragraph a class which is called as
6:48
card text. it will be also be having a
6:52
text muted class.
6:55
So if you refresh it uh inside the
6:57
browser you will now see this is the
7:00
login with PHP session.
7:05
So
7:08
then we will have these buttons out
7:10
there guys this paragraph and the signin
7:12
button.
7:15
So simply
7:18
include this. So we have uh login.php.
7:21
If you click this link here, you will be
7:23
able to go to if you click this signin
7:26
page, you will able to go to login.php.
7:28
We will haven't created this file yet.
7:31
After this guys, after this is over
7:33
right here, basically
7:36
this div
7:38
is over. Now we will have another column
7:40
class.
7:42
This time again these will have these
7:45
classes. Card center, card header,
7:48
H3 card title
7:54
and this time this will be for register.
7:57
So there will be two sections out there.
7:59
First is for the login and second is for
8:01
the register column and this is our div
8:04
section.
8:07
So after this guys what we need to do is
8:10
that we again need to write the card
8:15
body class. So right here simply include
8:18
this
8:20
and we just need to rename this to index
8:23
dot PHP file guys. So just rename this
8:26
to PHP.
8:29
So if you refresh it now you will have
8:32
two forms out there. First is for login
8:34
and second is for register. You will now
8:36
see this is if you click the signin page
8:38
you will be redirected to login and if
8:41
you click the register page we haven't
8:43
created yet this register dot php file.
8:46
So
8:48
we need to create this. So right here
8:54
this div class is over.
9:03
Let me paste all this code here.
9:13
So these are this is the homepage guys.
9:15
Basically when you open the page here
9:17
you will have two sections either you
9:19
can go to login or register. These are
9:21
the two sections out there which is
9:23
there for you. So if you're watching it
9:26
for the very first time guys then please
9:27
hit the like button subscribe the
9:29
channel as well.
9:31
And if you have any sort of doubt you
9:33
can ask in the comment section. If you
9:36
open the page you will now see this is
9:38
login and register. You will now see
9:41
login register and this is sign in and
9:44
this is sign up. So you can now see
9:47
these are the two sections out there
9:49
sign in and sign up. So after this guys
9:52
what we need to do is that we need to
9:54
create our uh login dot php class
10:00
and also our uh
10:03
register dot php class.
10:13
So this class is currently empty guys.
10:15
So right here we need to go to our
10:18
PHP myadmin and we need to create a new
10:21
database here. Basically this database
10:23
will be responsible for.
10:27
Click on new. Create a new database.
10:29
Brand new database which will be
10:33
session login
10:37
or we can say session system
10:41
and click on the create option. So this
10:43
will create this database for you. And
10:46
inside this guys we need to create a
10:48
table. Basically, if you follow the blog
10:52
post, you will now see the table will be
10:55
right here.
10:58
You can go to the description of this
11:00
blog post and simply you can paste this
11:03
SQL query inside your uh right here.
11:07
Simply paste it.
11:10
So, it will have the ID which is a
11:11
primary key with auto incremented. Then
11:13
we have username, email address,
11:15
password and primary key is the ID.
11:18
Simply click on go and this will create
11:21
your table. So if you see the structure
11:23
of your table, users table will be there
11:26
and it will contain four fields out
11:28
there. ID, username, email and password.
11:30
So these are the four fields out there
11:32
which is there inside this. So uh you
11:36
can see that.
11:38
So if you're watching it for the very
11:40
first time guys, please hit the like
11:41
button, subscribe the channel as well. I
11:44
see people are not liking the stream.
11:47
So please hit that like button,
11:49
subscribe the channel as well if you're
11:51
watching it for the very first time.
11:54
Uh let me see why people haven't joined
11:56
it for this live stream. I think YouTube
11:59
is the culprit here. They don't send the
12:02
notification and it is so disappointed
12:04
to see that.
12:07
Please hit that like button guys.
12:08
Subscribe the channel. This is a session
12:10
based login system inside PHP
12:17
PHP and MySQL.
12:20
So, hit that like button. PHP and MySQL.
12:30
So, hit that like button guys. Subscribe
12:32
the channel as well.
12:43
So now our table is created guys. So now
12:46
we just need to create the connection
12:48
between the PHP script and the MySQL
12:50
database. So right here we will make
12:52
this file which is called as connection
12:54
dot PHP. And right here you just need to
12:56
write the code here for the connection.
12:59
This is very easy. We will make the
13:01
connection local host. Sorry, host will
13:04
be equal to local host.
13:08
Uh username will be equal to root
13:13
and uh password is empty
13:16
and database is uh session system.
13:23
Put a semicolon
13:25
every statement at the end.
13:31
So basically now we can get the
13:33
connection guys. So we can create a
13:34
connection. So mysqlite
13:40
pass this information.
13:50
So database. So we can now check the
13:55
connection here. Basically if the
13:56
connection uh
14:02
has any sort of error in that case we
14:04
can die and save
14:09
uh cannot connect to database.
14:16
So four people are watching the stream
14:18
guys. So we are building a sessionbased
14:20
login system. So we have written the
14:22
connection code for connecting to the
14:24
database local host root. This is a
14:26
database name session system. So if the
14:29
connection is not available then we will
14:31
simply cannot connect to database.
14:33
That's it.
14:36
So you can go to the blog post guys. So
14:39
here we have given all the source code.
14:41
This is my SQL I connect. This is a
14:44
method.
14:46
MySQL I connect. Sorry. This is a method
14:48
we will use guys. My SQL I connect. It's
14:50
a object oriented way of connecting to
14:53
the database.
14:56
So this script is also done which is
14:58
connection dotphp. So now we will first
15:00
of all create the user. So right here
15:04
basically we will insert the users. So
15:07
inside your register dot php guys we
15:10
will
15:14
first of all create a simple form.
15:19
So inside register do.tphp guys this
15:21
will be a simple bootstrap form which we
15:23
will create here. Basically just go to
15:25
your register dot php which is currently
15:27
empty. So right here just you need to
15:31
make a div which will have a class of
15:34
container
15:36
uh column 4 and we will give it a border
15:39
rounded bglight. These are all bootstrap
15:42
classes guys. So make sure
15:48
uh BG light and also margin from the top
15:52
five
15:54
and also we will be giving a style
15:56
attribute to it BSPG opacity
16:02
will be equal to 0.5 I think. Yeah 0.5.
16:08
So these are all bootstrap classes guys.
16:10
So right here basically we will have a
16:13
H1 tag.
16:15
So inside the H1 tag will simply say uh
16:19
sign up.
16:21
So if you just rewrote your application
16:23
guys go to your
16:26
homepage there will be two sections out
16:29
there. If you click the sign up you will
16:31
be redirected to this page here which is
16:34
saying here sign up.
16:40
So also inside index.tphp guys. So
16:43
wherever you are including this right
16:46
here
16:51
I think we need to include this uh file
16:55
guys at the very top
16:59
uh include
17:01
uh header dot php to get the styling.
17:08
So just do this now you will get your
17:11
bootstrap styling as well. You can now
17:13
see sign up form this is.
17:18
So inside this guys we will have a
17:20
simple
17:22
it will be in the center position. So
17:24
you will be give it a class which is
17:26
text center
17:28
and inside this we will have a href hr
17:31
tag here. Then we will have a simple
17:34
form. Action will be nothing. Method
17:37
will be post
17:39
and uh just close this form. And right
17:43
here guys we will be having a div which
17:45
will have a class which is margin bottom
17:48
three.
17:51
So inside this div guys guys we will
17:53
have a simple label
17:57
and we will be giving a for attribute to
18:00
it. So here we will write the name and
18:02
we will be giving a class to it of form
18:05
label. So these are all bootstrap
18:08
classes. So this will be for username
18:10
guys
18:13
and then we will have a simple input
18:15
field type text and uh class will be
18:18
equal to form control name will be equal
18:21
to name and simply the placeholder will
18:24
be equal to enter username.
18:30
So if you refresh it guys now you will
18:32
see a input field out there which will
18:34
be for username. You can now see.
18:38
So this is the bootstrap field guys. You
18:40
will now see and autocomplete we will
18:43
set it to off. Autocomp complete is off.
18:46
Please hit the like button. Subscribe
18:47
the channel as well guys. And it should
18:49
be required for validation.
18:53
And similarly guys we will do it for the
18:56
other fields as well. We will have
18:57
another div tag MB3 margin bottom three.
19:03
Inside this we will again have a simple
19:05
label here. Basically this label will be
19:08
for this time email address and class we
19:12
will once again give it a form label.
19:14
This will be for email address
19:19
and then we will have a input type this
19:21
time of email address. Class will be
19:23
again form control.
19:26
Name will be email and the placeholder
19:28
will be simply enter email
19:33
and autocomplete will be simply to set
19:36
to off and it should be required. So if
19:39
you now refresh it guys you will now see
19:42
a second field out there for email
19:44
address. This is username. This is email
19:46
address. Please hit that like button. 10
19:48
people are watching the stream. I am
19:49
seeing people are not liking the stream.
19:52
Then we will be having a next field guys
19:54
which will be for uh the full name I
19:57
think.
19:59
So close this div.
20:02
Close as div. And uh next field is for I
20:06
think is for the password. So you will
20:09
also need a password guys. So label for
20:12
password
20:14
and class will be a form label.
20:18
This will be for password.
20:20
Close the label and this time input type
20:23
password.
20:26
Class will be of form control. Name will
20:30
be of password
20:32
and placeholder we will say
20:36
enter your password
20:40
and autocomplete will be set to off
20:45
and this will be required as well.
20:50
So if you now refresh it guys, there
20:52
will be a password field as well. You
20:54
will now see this is a password field.
20:57
Uh let me uh disable this guys. So
21:03
we have these three fields out there
21:05
guys. Now we will have to have a button
21:07
basically to submit this form. So
21:11
basically to submit this form. So this
21:13
is very easy.
21:17
We can even add this uh small tag as
21:20
well guys be after this input field.
21:24
So whenever you do this after this you
21:26
can add this small your email is safe
21:29
with us if you refresh it.
21:32
So this is a small tag. You can see that
21:35
your email is safe with us. And
21:37
similarly you can add this your do not
21:40
share your password.
21:45
Just after this uh input field out there
21:48
simply paste it. So we have given this
21:50
class text muted. This is a bootstrap
21:53
class.
21:55
So do not share your password. Then we
21:57
will have a simple button out there guys
21:59
which will let you submit the form. So
22:01
after this we will have a div which will
22:05
have a class of margin bottom three and
22:09
uh just close the div here. And right
22:12
here guys you will have a input type
22:14
will be of submit and name will be of
22:16
signup
22:19
and the value will be simply of
22:21
register. So here we it will let you
22:24
register it.
22:28
So input type submit and uh this we'll
22:31
be having a bootstrap class guys which
22:33
will be of btn btn primary. So these are
22:37
the boot bootstrap classes for the
22:40
button.
22:41
Just refresh it you will now see
22:44
register is there.
22:48
So this is your uh registration form
22:50
guys. So if you uh put these values here
22:53
and we need to now insert these values
22:56
inside the database that we created. So
22:58
if you see go to PHP myadmin we have
23:01
created this database which is called as
23:03
session system. Inside this we have this
23:06
table which is currently empty right
23:08
here. We need to uh insert the values.
23:10
For inserting the values guys it's very
23:13
easy. We need to write the PHP script
23:15
guys for this. So just write here go to
23:18
your uh if uh blog post which is given
23:22
in the description of this live stream
23:24
guys. So after doing this
23:28
inserting the users. So this is the code
23:30
required for this guys inside your
23:33
register.php.
23:35
So simply I will write this step by
23:37
step. So go to your register.php and the
23:40
add right right here at the very top
23:42
after you include this uh header. PHP we
23:45
will write some more PHP code here. So
23:47
please hit that like button, subscribe
23:49
the channel as well guys. So just put
23:50
the PHP tag here and basically guys we
23:53
will be writing uh we will having a if
23:56
condition. So here we will be checking
23:58
that if is set and here we will check
24:01
that basically if the form is making a
24:05
post request sign up.
24:09
So closely notice guys we are making a
24:11
post request inside this form. The
24:13
method is post here and this uh button
24:16
that we have given is name attribute
24:18
which is called as signup. So we are
24:20
just checking in the PHP script that if
24:22
this name is set if the name is set
24:25
inside this if condition it will return
24:27
true. So we will execute this
24:30
application. So we will get the name
24:31
here by simply dollar.post
24:34
the name attribute which is we have
24:36
given to the username which is name.
24:38
Similarly the email address we will get
24:40
the email as well like this. We can get
24:44
the email as well like this.
24:46
And also we can get the password like
24:49
this dollar post password.
24:54
So these all are getting the name
24:57
attributes you have given in the HTML
25:00
guys. You can see name, this is email
25:02
and this is password. So these are all
25:04
the name attributes. So after getting
25:06
all these three things guys name, email
25:07
and password we need to insert this
25:09
inside the query insert to MySQL
25:12
database. So now to insert this we will
25:15
simply write a SQL query which is uh we
25:18
will write SQL. So we will say insert
25:20
into the table name which is in this
25:22
case is users. So here we will say the
25:26
column name is username, email,
25:28
password. So these are the uh column
25:31
names guys basically and then we need to
25:33
provide the values which needs to be
25:35
inserted. So the values will be simply
25:37
coming here like this. The values are
25:40
the name variable which we have created
25:43
the email variable and the password
25:46
variable. That's it.
25:50
So this is the values guys. You can now
25:53
see.
25:58
So we need to make sure it is inside
26:00
single quotes. So what we can say is
26:02
that
26:04
in single quotes
26:06
uh
26:16
uh just write this query like this.
26:20
So just copy this and basically paste it
26:22
here
26:26
in single quotes. You will see we are
26:28
passing the variables guys name, email
26:30
and password. You will now see
26:39
in single quotes.
26:46
Uh now we need to insert this. So we
26:48
will execute this query which is uh by
26:50
the method which is called as MySQL I
26:53
query and here we will pass the first of
26:55
all the connection object and the query.
26:58
For passing the connection object guys
27:00
we also need to include the connection
27:02
file which is uh you can include this
27:05
inside header.php right here at the very
27:07
top. So just you can include this file
27:11
which is PHP include
27:14
once the connection dot PHP file. So
27:18
just include this file guys right here.
27:23
So we are including this file
27:25
header.php.
27:27
So this connection object if you see
27:30
inside the connection dot PHP we have
27:32
this con object. So we are using this
27:34
object right here to add this user. So
27:37
now we will have a if condition guys to
27:39
check that if the user is successfully
27:41
added or not. So we will compare this
27:44
variable.
27:46
If it is not added then we will echo out
27:48
that uh something went wrong
27:52
and then we will just concatenate the
27:56
error which taken place passing the
27:58
connection object.
28:01
So that's it guys. So else we can simply
28:04
say that the user has successfully been
28:06
added
28:09
or we can redirect the user basically to
28:12
a location.
28:14
We can redirect the user to login.php.
28:18
So this is the code required to redirect
28:21
the user guys. So we are providing a
28:23
header. You are using the header method
28:25
location. We are providing login dot
28:26
php. So if I try to execute this code
28:30
guys try to go to uh register.tphp. PHP
28:32
and try to enter a username here. Let's
28:34
suppose Geeky Gotham. Enter a email
28:36
address here. Geeky Gotham 1987
28:40
at the rategmail.com and enter the
28:43
password.
28:45
Click on register. So you will see
28:47
something went wrong. Unknown column
28:50
email and field list.
28:52
Uh let me see if you check the structure
28:55
of your thing it is called as email.
29:01
So what is the problem here? Let me see.
29:03
In register dot php unknown column
29:13
uh dollar post.
29:27
Uh let me just paste it guys and once
29:30
again run this.
29:46
U
29:49
right here. Let me paste it.
30:02
So if you just refresh it guys once
30:04
again. Now
30:06
enter the username, enter the email
30:08
address,
30:10
enter the password.
30:12
Click on register. Something went wrong.
30:15
Unknown column email and field list.
30:19
Uh what is the
30:22
insert into users username email
30:38
uh name
30:40
email
30:45
password.
30:49
So 11 people are watching the stream
30:51
guys. Please hit the like button,
30:52
subscribe the channel. So enter the
30:54
email address,
31:03
email, password. This is ID incremented.
31:13
So if you're watching it for the very
31:14
first time guys then please hit the like
31:16
button. Subscribe the channel. So only
31:18
three likes are there guys. Please hit
31:19
that like button also. I'm just
31:21
requesting you guys just increase this
31:23
like number to 10 likes. If you want to
31:26
also subscribe the channel as well.
31:29
Uh why I think it is creating this
31:32
problem. Let me echo out
31:42
echo out the email address. So if I echo
31:46
out the email address guys, so what will
31:48
the case?
31:56
Uh the email address is getting
32:01
insert into users.
32:05
Uh
32:09
email password
32:14
username.
32:16
Email password.
32:25
I don't know guys why this error is
32:27
taking place. So
32:33
if you refresh it now
32:42
enter the password here again. This
32:44
something went wrong. Uh why something
32:48
went wrong?
32:54
Uh unknown column email in field list.
33:26
So again refresh it guys. So put the
33:30
email address here.
33:54
Uh I think it is saying that your uh
33:58
username email address
34:05
we can try to edit this. So we can
34:08
change it.
34:10
Let me change it to email. Click on save
34:22
again. Let me refresh it. Hopefully this
34:24
time it will work.
34:35
I think sometimes you need to clear out
34:37
the catch as well. So empty catch and
34:40
hard reload. And this time hopefully it
34:43
will work.
34:46
Just empty catch and hard reload. Empty
34:49
catch.
35:10
So what we can do is that guys we can uh
35:12
hardcode the values inside this. I don't
35:15
know why it is creating this problem.
35:16
Just click on insert and basically
35:18
insert a username here
35:21
and uh enter a email address
35:27
or what we can do is that guys uh
35:29
basically what we can do go to the
35:31
structure here
35:34
you can drop this field just delete this
35:36
field here because it is creating this
35:37
problem. So we have three fields out
35:39
there. So inside this register we don't
35:42
have to have adding this email address.
35:45
So simply delete this. So we are not
35:49
getting this email field.
35:53
So we are just
35:55
inserting the
35:59
name and the password. Name is equal to
36:01
username.
36:03
And if you now try to refresh.
36:10
So enter the username and enter the
36:12
password.
36:14
So you can see it is redirecting me to
36:16
login.php and if I check here basically
36:19
one entry should have been added
36:27
name password.
36:30
Insert into users username password. The
36:33
values are this one.
36:53
Uh any questions guys you need to ask
36:55
here or in the live chat you can ask it
36:58
in the comment section.
37:00
So why it is not inserting it? Let me
37:02
see guys. So this is the code here.
37:16
So if you see basically uh
37:25
in single quotes let me see
38:10
And if you check the connection guys, oh
38:14
this is a problem guys.
38:17
If you see we have put the wrong
38:19
database name that's was that was the
38:21
problem. So login system we have put
38:24
here. So this needs to be session
38:25
system. So that was the problem guys. So
38:29
why
38:30
it was so hard work to find out that
38:32
error. So if you again go to local host
38:36
login system T
38:48
local host.
38:54
So enter the username and now the
38:57
password. Click on register and
38:58
hopefully this time you will now see
39:01
that username password is successfully
39:04
inserted
39:06
and uh I think we can now include the
39:08
email address as well. So simply
39:10
controls it.
39:19
So we can add this field here. Basically
39:21
go to the structure and uh right here we
39:25
need to add a new one field here after
39:29
username click go. So this will be email
39:32
address and it will be again var
39:37
and simply you need to add this fee.
39:43
Click on save. So now okay var is 256.
39:50
So now you will see uh basically I will
39:52
delete this field here.
39:55
And now once again we will create a new
39:58
entry
40:00
sign up username and now we will enter
40:03
the email address
40:06
password.
40:08
So now you can see that it is
40:10
successfully inserting the data. You
40:13
will now see. So now we have uh
40:15
insertion is taking place guys. We are
40:17
successfully able to insert data. So it
40:20
is redirecting us to login.php.
40:23
And now we need to store this data
40:25
inside a session guys which is this
40:27
data. So we will now now try to write uh
40:31
also our profile page. So just create a
40:34
dashboard.php
40:36
page guys. This is the profile page.
40:38
After the user is uh logged in
40:40
successfully, this will be redirected to
40:42
this page dashboard. PHP. So inside
40:45
dashboard.php guys, first of all, we
40:47
need to write the login dot php. This
40:49
file is currently empty. So we also need
40:52
to basically check whenever
40:56
the user is successfully inserted.
41:00
Instead of redirecting it to login.php,
41:02
you can directly insert them to
41:04
dashboard.php. dashboard.
41:07
So now we need to create this profile
41:09
page guys. This will be the profile page
41:11
of the user. So right here basically
41:15
inside this this is a code required for
41:17
this file. So simply I will copy this
41:19
and paste it here and uh
41:23
just write step by step. So inside the
41:25
table we will be printing out all this
41:27
information. So first of all we will say
41:31
in PHP we will say session. Start we
41:35
will say we will start the session
41:38
session
41:40
start
41:43
like this. So we will use this method
41:45
guys and also we will include the uh
41:49
header dot php file as well. So include
41:52
once header dot PHP
41:57
and right here guys you will again have
41:59
the PHP tag here.
42:04
So inside this guys we will first of all
42:06
check that if is set
42:11
and uh we will say that dollar session
42:16
id if the id is not set in that case
42:19
guys we will uh basically
42:23
uh redirect the user to login page. So
42:26
basically it means that if the primary
42:28
key is not set, if the user is not set,
42:30
in that case, if the session is not set,
42:33
in that case we will redirect the user
42:38
back to the homepage. So we now need to
42:41
have some
42:43
HTML also guys right here inside this.
42:46
So we will simply say a div tag will be
42:48
there which will have a bootstrap class
42:51
of container
42:53
call 12 border rounded
42:57
empty3.
43:01
So close this div tag. These are all
43:03
bootstrap classes. Inside H1 guys we
43:06
will simply
43:09
say welcome. This is your dashboard.
43:14
This is your dashboard.
43:18
So we will be attaching some of
43:19
bootstrap classes guys which is MT3
43:23
text center.
43:27
Then we will have a simple H horizontal
43:31
line.
43:33
And then inside H4 tag we will simply
43:36
display the
43:39
uh we will echo out from PHP which is
43:42
echo from the session
43:46
the name. We will echo out the name of
43:49
the user like this.
43:56
We will close out the PHP tags. That's
43:58
it.
44:00
So if you try to refresh the application
44:02
guys, so what will happen now? If you
44:04
create a new user, if you go to
44:06
register.tphp
44:11
and if you enter a username here, enter
44:13
the email address,
44:16
enter the password.
44:18
So it will again redirect to the login
44:21
page. Let me see.
44:24
Inside
44:27
register dot php we are redirecting it
44:29
to dashboard.tphp.
44:37
Simply save this. So autosave is on.
44:46
So just clear out the catch guys. It can
44:48
sometimes be a problem.
45:17
So inside register PHP guys we are uh if
45:20
you see register dotphp we are
45:24
redirecting the user to dashboard.php.
45:27
So why it is not
45:31
I think guys this is all this changed to
45:34
login.
45:40
Please hit the like button guys.
45:42
Subscribe the channel as well if you're
45:43
watching it for the very first time.
45:50
So first of all we will write the
45:53
login.php file guys.
46:02
So inside login dot.php file guys we
46:04
will simply have a simple form here
46:06
basically
46:08
in order to log to the user which is
46:11
email id and password. So in order to uh
46:14
include the styling we will include the
46:17
PHP file right here at the top which is
46:20
include once
46:22
uh header.php PHP.
46:31
So this will be your login form where we
46:33
can sign in with your email address and
46:35
password.
46:36
So
46:38
now we will write the simply the PHP
46:40
code guys. First of all here inside the
46:43
file we will start the session.
46:48
So session start
46:57
So here inside this guys we will first
46:59
of all compare the
47:02
again use the is set method.
47:05
So in this time we will pass the session
47:07
variable which is session
47:11
ID.
47:13
If it is not set then we will once again
47:18
uh
47:24
uh let me write it the login page.
47:27
Sorry. Uh
47:35
for the login guys it's very simple.
47:37
Basically
47:39
we need to start the session at the very
47:41
top and right here
47:43
we need to check for uh is set and uh we
47:47
need to check if the submit button is
47:50
clicked. So we have given a name
47:52
attribute to it of sign in. So if it is
47:54
clicked that after this we will get the
47:56
email address write this dollar post
47:58
email.
48:03
We will also get the password
48:06
dollar post password.
48:08
After getting all these two things guys,
48:10
we will simply execute a simple query
48:13
here which will be called as select. We
48:15
will select everything from this table
48:17
of users and we will make a condition
48:20
here where email address is equal to the
48:23
past email address.
48:25
So
48:28
in single quotes we will write email
48:31
and we will put a condition and and
48:34
password will be equal to
48:38
password.
48:46
So put a semicolon. So we have written a
48:48
simple SQL query guys. So we will put a
48:52
uh the user we will get the user. We
48:54
will execute this query MySQL I query
48:57
pass the connection object and also the
48:59
query as well. Here we will check in the
49:02
if condition guys. If the user is empty
49:04
then in that case we will simply say to
49:06
the user that your credentials are
49:08
wrong.
49:10
You are invalid user.
49:14
That's it.
49:17
If the credentials are right guys, so we
49:20
will simply use the while loop and
49:23
basically we will show inside the table.
49:28
We will fetch it fetch the result in a
49:31
associative array. MySQL I fetch array
49:33
and where we will pass the user object
49:36
and inside this we will get the user ID.
49:39
User ID is equal to row ID and we will
49:44
also get the username like this.
49:46
username is equal to row
49:50
username.
49:52
We will also get the user email which is
49:55
equal to row uh email address. These are
49:59
all the column names and also we will
50:02
get the user password which will be
50:04
equal to
50:05
uh user password.
50:08
So after getting all these four things
50:10
guys we will simply uh first of all
50:13
compare it in if condition here. If the
50:16
user email address is equal to the email
50:19
address which is found and also the
50:21
password
50:23
if the user password is also equal to
50:25
the past password
50:28
then in this case we can simply set our
50:31
session variables. So we will say set
50:35
the session variables. We will set the
50:37
ID to the user ID which we have got
50:41
and also we will set the you uh username
50:46
to the this one user name
50:52
and also we will set the email address
50:54
session. So in this way guys you can set
50:56
the session variables. This is very easy
51:00
and uh we can even set our password.
51:07
So password will be equal to user
51:09
password.
51:15
So password need not have to be set
51:17
guys. So we can don't need to set
51:18
password. And here we can redirect the
51:20
user to the location which is called as
51:22
dashboard dot php. and question mark you
51:25
will say user id is equal to user id is
51:30
equal to
51:32
dot user id
51:36
put a semicolon that's it so here we are
51:39
passing a simple query parameter here
51:41
user ID in the address bar guys whenever
51:44
we are redirecting the user dashboard
51:46
php question mark user ID is equal to
51:48
the past user ID that's it in the else
51:52
scenario if basically if it is not equal
51:54
equal to then we can simply again target
51:58
uh redirect the user to the login page
52:00
login.php PHP. That's it. This is the PH
52:04
PHP script guys for the login page.
52:06
That's it. So if I execute this
52:08
application now, go to login.php. If I
52:11
try to enter a wrong credentials here
52:14
and wrong password again, it will
52:17
redirect me to login page. If I enter
52:20
here correct credentials, let me
52:23
um enter a email address here which is
52:26
correct which is present inside this
52:28
table.
52:31
and also enter a password. This one
52:38
click sign in. You will see welcome.
52:39
This is your dashboard. So this is your
52:41
dashboard page guys. You will now see
52:42
the address where this user ID is passed
52:44
to it which is the two user ID which is
52:48
correct because this user ID is two. So
52:52
we are printing this data inside this
52:53
table like structure. Guys, you will see
52:55
this is the username and also guys we
52:58
need to have a log out button also which
53:01
will allow the user to simply log out
53:03
from the page. So
53:07
uh we will be simply be having uh
53:13
we will have a log out button guys which
53:15
will actually make the user log out. So
53:17
go to your dashboard.php page guys. So
53:20
wherever we are doing this stuff here we
53:23
will also have a log out button.
53:25
Basically this will allow the user to
53:27
simply log out from the page. So right
53:31
here inside your blog post guys you will
53:33
simply have
53:37
simply paste this code here.
53:40
So right here you need to simply uh
53:45
after this we will have a simple table.
53:47
Here we will display this information
53:49
inside a table bootstrap table. So we
53:53
will be giving a class to it table
53:55
striped table bordered
53:59
table
54:01
all these are bootstrap classes table
54:04
hover.
54:11
So this is a table guys. So basically we
54:14
will have three columns inside this
54:16
table.
54:23
So inside this table we will have three
54:26
columns out there. So table row.
54:36
So three columns will be the actual user
54:39
ID, username and email address.
54:43
And inside table body we will
54:47
we will have the table body tag.
54:52
So basically we will be printing out the
54:55
information. So the first one will be
55:03
will be equal to echo.
55:06
We are we will be getting through the
55:08
session variables which is set already.
55:10
This will be user ID.
55:16
So like this. So
55:19
simply now we will be copy pasting it
55:22
for the username and email address. This
55:25
will be username
55:29
or simply name. This will be for email.
55:33
That's it.
55:36
So if you refresh it guys, if you go to
55:38
the page here,
55:40
you will now see this is ID, this is
55:42
username, this is email address. Then we
55:45
will also have a simple log out button
55:47
basically to log out from the page.
55:55
So after this table,
56:00
this will be a simple post request guys.
56:03
This will be gone to the same page. This
56:05
is a button here. We have given this
56:06
bootstrap styling to it.
56:10
Sign out button will be there. If you
56:11
click this button, you will be able to
56:13
redirect to the login page. So we need
56:16
to write this code guys which is very
56:18
easy. We need to destroy the session. We
56:21
need to delete the data which is present
56:23
inside the session. So right here we
56:25
will uh write this PHP code. So this
56:28
will destroy
56:31
session.
56:33
So in order to destroy a session guys,
56:35
it's very easy. Basically we will first
56:38
of all check that if the login button is
56:40
pressed using this method if is set
56:44
dollar post
56:46
uh sign out.
56:49
So here if the sign out button is
56:51
pressed guys we will simply call this
56:53
method which is session destroy. So it
56:57
will destroy all the data which is
56:58
present and then we will redirect the
57:00
user back to the login page
57:05
uh index.tphp.
57:09
So now if you refresh it guys click the
57:12
sign out button you will be redirected
57:14
to back to if you sign up a new user you
57:17
can sign up the user. Let me enter a
57:20
username, email address.
57:25
Uh let me enter a password.
57:29
Click on register. So you successfully
57:32
registered. And now if you enter the
57:35
credentials
57:37
correctly,
57:41
click on sign. So you can now see
57:43
welcome. This is your dashboard. This is
57:45
a username, email address and a sign out
57:48
button like this.
57:52
So this is a complete system guys. We
57:54
have developed using PHP sessions which
57:57
is very much good in order to store
57:59
information in between pages.
58:04
So you can now see
58:08
so if you like this video guys then
58:09
please hit that like button subscribe
58:11
the channel. You can see only five likes
58:13
are there. So increase these like number
58:15
to 10 likes.
58:17
Also hit the subscribe button as well.
58:20
All this information is stored inside
58:22
this PHP MySQL database guys. You will
58:24
now see.
58:27
So there is one little vulnerability of
58:31
this application. We are not storing
58:32
passwords.
58:34
We are not encrypting these parts. So it
58:36
is just stored in as plain text. But I
58:38
just wanted to show the concept of PHP
58:40
sessions.
58:42
You can just use any encrypt function to
58:44
encrypt these passwords.
58:50
You can sign up a new user like this.
58:55
All the source code guys is given in the
58:57
description of this blog post. I have
58:59
given in the live chat as well. The full
59:01
source code. You can click this and go
59:03
to the full source code.
59:07
Uh please hit that like button also if
59:09
you're watching it for the very first
59:11
time.
59:44
Uh alone status is asking can we still
59:46
access the dashboard page by simply
59:48
writing in the address bar.
59:52
Uh I think no because we are setting the
59:54
session variables. Let me show you.
59:58
Let me do this. If you're trying to do
1:00:01
this,
1:00:04
you are telling me that if I directly
1:00:07
write if I go to the homepage.
1:00:24
So if you copy this URL and if you sign
1:00:26
out and directly enter this
1:00:30
No, it will redirect back to the login
1:00:32
page because session data is destroyed
1:00:35
after you click that button of sign out.
1:00:38
So if you try to directly enter that
1:00:40
URL, you will not be able to access that
1:00:43
page. For that you need to enter the
1:00:46
email address and password. So that is
1:00:48
completely secure. So you can't access
1:00:52
that page.
1:00:55
But still it is not a good practice
1:00:56
showing the user ID which is this one.
1:00:58
user ID. So that is not a good practice.
1:01:01
So we need to fix that also. If you are
1:01:04
building a secure login system, you need
1:01:06
to hide this value as well. So simply
1:01:08
the user must see dashboard.php.
1:01:13
This is just a very basic system. It's
1:01:15
not a recommended one that you want
1:01:17
build in production level. It's just a
1:01:20
basic session based system which we have
1:01:22
developed.
1:01:46
Please hit that like button guys. Why
1:01:47
are you not liking the streams? Only
1:01:49
five likes are there. It is not great by
1:01:53
any sort of imagination. and I'm putting
1:01:55
the hard work. You're not hitting that
1:01:56
like button. So, please hit that like
1:01:59
button. Subscribe the channel as well.
1:02:02
Subscriber goal if I show you basically
1:02:05
it is set to again
1:02:08
it's very low 169. So, try to reach up
1:02:11
to 200 subscriber goals as soon as
1:02:14
possible.
1:02:16
So, also hit that like button as well if
1:02:18
you're watching it for the very first
1:02:20
time.
1:02:25
If I see this uh go to the filter here
1:02:28
and and
1:02:32
see the properties here Microsoft Real
1:02:36
Tech.
1:02:45
Only seven likes are there guys.
1:02:47
Increase it to 10 likes if you can.
1:02:52
Live watching is why it's low.
1:03:01
So which content you like guys in this
1:03:04
channel JavaScript or PHP
1:11:08
So I've started a poll guys. Basically
1:11:10
you can tell me in the just this live
1:11:12
poll which videos you want to see
1:11:14
because this YouTube live watching is
1:11:15
pretty low whenever I make videos and uh
1:11:18
last two three live streams. So please
1:11:20
tell me in this live stream whichever
1:11:22
videos you want to see either month
1:11:25
stack mean stack nodejs express and PHP
1:11:28
or MySQL
