In: Computer Science
Write a log-in log-out session using PHP, CSS, and HTML only. NO MYSQL and JavaScript allowed.
1. Need an HTML form and a login form; separate file.
2. need a CSS file to make it look good
3. Must store visitors' info in a .txt file and validate from there if the user exists before granting access to the dashboard. If the user does not exist, render the form to signup. If the user exists take them to their dashboard. Just write granted access to the dashboard and no need to do anything else.
4. All the pages must have the same theme(looking same in the background using CSS)
Step:1
We Have To Create Some required Files For Our Login System .
Files are :-Create all files and put together in same folder
Now I am Putting Code For All files:
CODE FOR index.php
<?php
session_start();
include "style.php";
if (!isset($_SESSION['Loggedin'])) {
header('Location: login.php');
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>DashBoard</title>
</head>
<body>
<section>
<div>
<h1>Dash Board-Logged IN</h1>
<p><?php echo $_SESSION['email']."<br>". $_SESSION['company'] ; ?></p>
<form action="logout.php" method="post">
<input type="submit" name="Logout" value="logout">
</form>
</div>
</section>
</body>
</html>
CODE FOR login.php
<?php include "style.php";?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<section>
<div>
<h1>Login</h1>
<form action="auth.php" method="POST">
Username: <input type="text" name="username" required="">
<br />
Password: <input type="password" name="pass" required="">
<br />
<input type="submit" name="submitlogin" value="Login">
</form>
</div>
</section>
</body>
</html>
CODE FOR auth.php
<?php
session_start();
$userN = $_POST['username'];
$passW = $_POST['pass'];
$userlist = file ('user.txt');
$_SESSION['Loggedin'] = false;
foreach ($userlist as $user) {
$user_details = explode('|', $user);
if ($user_details[0] == $userN && $user_details[1] == $passW) {
$_SESSION['Loggedin'] = TRUE;
$_SESSION['email'] = $user_details[2];
$_SESSION['company'] = $user_details[3];
break;
}
}
if ($_SESSION['Loggedin'])
{
header('Location: index.php');
} else {
echo "<br> You have entered the wrong username or password. Please try again. <br><a href='login.php'>Goto Login</a>";
}
?>
CODE FOR logout.php
<?php
session_start();
session_destroy();
// Redirect to the login page:
header('Location: index.php');
?>
user.txt
pete|petepass|[email protected]|Pete Enterprizes
john|johnpass|[email protected]|John Corporation