Part 5: Create your own Login System using PHP and MySql
5. Creating the Home Page
The home page will be the first page our users see when they’ve logged-in. The only way they can access this page is if they’re logged-in, whereas if they aren’t, they will be redirected back to the login page.
Edit the home.php file and add the following code:
<?php
// We need to use sessions, so you should always start sessions using the below code.
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: index.html');
exit;
}
?>
Basically, the above code will check if the user is logged in, if they are not, they will be redirected to the login page. Remember the $_SESSION[‘loggedin’] variable we defined in the authenticate.php file? This is what we can use to determine whether users are logged in or not.
Now we can add some HTML to our home page. Below the closing tag, add the following code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Home Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
</head>
<body class="loggedin">
<nav class="navtop">
<div>
<h1>Website Title</h1>
<a href="profile.php"><i class="fas fa-user-circle"></i>Profile</a>
<a href="logout.php"><i class="fas fa-sign-out-alt"></i>Logout</a>
</div>
</nav>
<div class="content">
<h2>Home Page</h2>
<p>Welcome back, <?=$_SESSION['name']?>!</p>
</div>
</body>
</html>
The above code is the template for our home page. On this page, the user will encounter a welcome message along with their name being displayed.
We need to add CSS for the home page. Add the following code to style.css file:
.navtop {
background-color: #2f3947;
height: 60px;
width: 100%;
border: 0;
}
.navtop div {
display: flex;
margin: 0 auto;
width: 1000px;
height: 100%;
}
.navtop div h1, .navtop div a {
display: inline-flex;
align-items: center;
}
.navtop div h1 {
flex: 1;
font-size: 24px;
padding: 0;
margin: 0;
color: #eaebed;
font-weight: normal;
}
.navtop div a {
padding: 0 20px;
text-decoration: none;
color: #c1c4c8;
font-weight: bold;
}
.navtop div a i {
padding: 2px 8px 0 0;
}
.navtop div a:hover {
color: #eaebed;
}
body.loggedin {
background-color: #f3f4f7;
}
.content {
width: 1000px;
margin: 0 auto;
}
.content h2 {
margin: 0;
padding: 25px 0;
font-size: 22px;
border-bottom: 1px solid #e0e0e3;
color: #4a536e;
}
.content > p, .content > div {
box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.1);
margin: 25px 0;
padding: 25px;
background-color: #fff;
}
.content > p table td, .content > div table td {
padding: 5px;
}
.content > p table td:first-child, .content > div table td:first-child {
font-weight: bold;
color: #4a536e;
padding-right: 15px;
}
.content > div p {
padding: 5px;
margin: 0 0 10px 0;
}
Now that we have our home page set-up, we can redirect our users from the authenticate.php file to our home page, edit authenticate.php and replace the following line of code:
echo 'Welcome ' . $_SESSION['name'] . '!';
With:
header('Location: home.php');
If you log in with the test account, you should see something like this:
This is a pretty basic home page. You can customize it to how you want now that you understand how it works.
Leave a comment
Recent Articles
- Grade Tabulator
- Part 6: Create your own Registration System using PHP and MySql
- Part 5: Create your own Registration System using PHP and MySql
- Part 4: Create your own Registration System using PHP and MySql
- Part 3: Create your own Registration System using PHP and MySql
- Part 2: Create your own Registration System using PHP and MySql