Web Application 2
Web Application 2
1. First, create a new folder for your project in Visual Studio Code and create a new file named
index.php.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<label for="username">Username:</label>
<label for="password">Password:</label>
</form>
</body>
</html>
<?php
// Start session
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
if ($valid) {
$_SESSION['username'] = $username;
header("Location: dashboard.php");
exit();
} else {
header("Refresh: 3; url=index.php");
exit();
?>
// Start session
session_start();
if (!isset($_SESSION['username'])) {
header("Location: index.php");
exit();
?>
<!DOCTYPE html>
<html>
<head>
<title>Student Dashboard</title>
</head>
<body>
<ul>
<li><a href="grades.php">Grades</a></li>
<li><a href="courses.php">Courses</a></li>
<li><a href="assignments.php">Assignments</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</body>
</html>
In this code, we have created a login page that allows the student to enter their username and
password. When the student clicks the Login button, the form data is sent to login.php.
In login.php, we retrieve the form data using the $_POST superglobal array. We can then validate the
username and password as needed. If the username and password are valid, we store the username in a
session variable and redirect the student to the dashboard page. If the username and password are not
valid, we display an error message and redirect the student back to the login page.
In dashboard.php, we check if the username is set in the session variable. If it is not set, we redirect the
student back to the login page. If it is set, we display the student's username and links to the grades,
courses, and assignments pages.