Module 2: Database Connection with Login
In this module, you will be learning on how to establish your database connection to your PHP code
and you’ll start working with user validation (LOG IN).
Since you have created your database already, it is now time to establish a connection of your PHP
to MySQL database.
a. In htdocs folder, copy the folder (SIS) attached in this document:
b. Create a file named [Link] and save it inside your folder.
c. Type the following PHP codes:
Note: $conn is a variable declaration that contains the hostname (localhost), user (root),
password (empty/blank) and the name of database (dbSIS).
d. Login source code:
d.1 Open [Link] inside your folder.
d.2 Insert the following PHP codes:
Note: The code inserted on the first line allows you to connect your database in your [Link]
file. It is very important whenever working with your database to include the connection on top
of the file as shown in the figure.
PHP with MySQL Modules prepared by SHERWIN M. CAACBAY 1|Page
d3. Continue inserting the following codes on the same file.
Note: Locate the form tag in [Link] and add two more attributes as shown in the figure. This
will allow your form to set the action and method attribute which will be used for your login
validation. Take note also of the name attribute of your two textboxes, the username and
pword.
d4. Continue working on the same file and insert the following PHP codes after the first line of
code inserted in d2.
Note: Make sure to type the exact code written on the above figure. If you are done, test your
work. Open your browser and type on the URL localhost/SIS/[Link]. Type the correct
username and password inserted in your table users, make sure to type it correctly and click the
login button. Once clicked, it should redirect you to main page/dashboard.
PHP with MySQL Modules prepared by SHERWIN M. CAACBAY 2|Page
d5. Working with invalid username and password.
Note: On line 40, insert the following CSS codes.
Note: On line 82, insert the following codes. Type it correctly, don’t forget to save your work and
go back to your login page and try inputting incorrect username and password. If you did it
right, your page should look like this:
PHP with MySQL Modules prepared by SHERWIN M. CAACBAY 3|Page
Note: Whenever incorrect credentials are entered; it should prompt the error message as shown
in the figure above. But if the user enters the correct credential (username and password) it
should automatically display the dashboard/main page of the system as shown in the figure
below:
d6. Working with [Link]
a. Create a new file and name it as [Link]. Make sure to save your file inside the
<includes> folder (SIS/includes).
b. Insert the following codes in [Link].
PHP with MySQL Modules prepared by SHERWIN M. CAACBAY 4|Page
c. Go to your folder includes and open the file [Link], change the default value
of href as shown in the figure below:
d7. Working with [Link]
a. [Link] will be used to check whether a user is currently logged – in in the system.
This will allow the page to check if there is a session registered in the system.
b. Open [Link] and insert the following line of codes;
Note: The inserted code allows you to register a session after logging in. The session name
is username and the value assigned to it is the value of $username (the value entered by
the user in the text box upon log in).
c. Now, create a new file and name it [Link] and save it inside the folder <includes>
(SIS/includes).
PHP with MySQL Modules prepared by SHERWIN M. CAACBAY 5|Page
d. Insert the following codes:
e. Open [Link] inside the admin folder and insert the code below:
f. Let’s try. Go to your browser and run the log in form.
Note: You must enter the correct username and password for you to access the main
page/admin dashboard.
g. Try to log-out from the system and see what happens next:
PHP with MySQL Modules prepared by SHERWIN M. CAACBAY 6|Page
h. Click on the arrow on top of the browser as shown in the image below,
Note: When the go back arrow is clicked, it should not return to the main page,
instead it will just stay in the log-in page since the session has been destroyed upon
log-out (see codes in [Link]). This will prevent unauthorized users from accessing
the system.
PHP with MySQL Modules prepared by SHERWIN M. CAACBAY 7|Page