In this lecture, we will create a login page for our panel to do so, we will be using HTML Forms
$_POST global variable of PHP and MySQL prepared statements.
If you are new to this concept, please learn more about them before continuing.
So let's start coding.
Open up your Atom Editor
And create a new file called login.php
In order to take input from the user, we need to create an HTML form.
To do so, open your html tag
and in order to create a form, you need to open a form tag.
our form tag will take two attributes.
First attributes, will be action.
We will keep it blank because we will send the request to the same page.
And second attribute will be the method, we will be using post requests so our method attribute will be POST
POST
Our form will have two input fields and one submit button, the first input field will be used for username
and second one for password.
Let's create the input fields first input field will be used for the user name, so it's type will the
text and it's name will be username and its placeholder will be username.
And the second input field will be in password type and it's name will be password and its placeholder
will be a bunch of stars.
And we need to create a submit button in order to send our request.
To do so we will create one more input tag, this time, the type will be submit and name will be submit.
And value will be log-in
This value will appear on the button.
And close form tag. Let's see if everything went well, open up your Firefox or another Web browser
and go to your localhost, slash login.php
As you can see, our form has been created.
What's next? We need to process the given username and password. To do so, we will be using PHP.
Open your PHP tag at the top of your login.php file, we need to create if statements
in order to check if given request is POST and username and password parameters has been set.
Let's create if the request method is POST and
Username
and password.
Parameters has been set, we will send the request, we will send the query to our database.
In order to send database queries securely, we will be using MySQL prepared statements. To do so,
We need the MySQL variable from the conn.php file because we will be connecting and executing queries
on database.
To include conn.php file into login.php file,
We will be using include function
Now we can use the $mysql variable, so let's create a prepared statement.
We will be using prepared methods
Our database query will be select * from users where username is question mark and password is question mark,
those question marks will be replaced with given parameters. To replace them,
We will be using method.
These data types will be string.
The first s will be used for the username parameter and second s will be used for password parameter.
So we are making sure that their data types are string.
Let's replace the first question mark, its user name, parameter and second question mark with password
parameter.
And and then execute the query by using execute function.
And we need to store results in order to check if the username and password is valid or not. To do so,
use store_result() function if the result of the query has more than zero rows
More than zero rows, which means that username and password exist on database.
Let's check if the result of the query has more than zero rows.
We will redirect user to index.php page, which will be the main page of the control panel
else,
We will print out an error message.
Wrong username and/or password.
And if the username and password is valid, we will start the session.
And we will set the username value
Into our session, which will be our username parameter,
and we will redirect user to index.php with using header function of PHP.
So one more thing to do in order to keep our passwords securely in database, we will be using hashes
To do so,
I will be using the MD5 hash.
Just encapsulate your password inside the MD5 function.
I know the fact that it's not the best hashing algorithm to use, but as long as you keep your password
complex enough the MD5 function will be enough to secure yourself.
So.
Let's go to the login page again and try to log in with a random username and password, as you can
see from username and password, message has been shown .
Since Oour database has no records yet,
We have no username and password which can be used for login.
So in the next lecture, we will create an username and password in database.
So that's it for this lecture.
See you in the next one.