Login finally works

So does logout
This commit is contained in:
Taylor Courage 2025-03-01 16:02:45 -05:00
parent 797859c683
commit 374197cdf5
12 changed files with 376 additions and 52 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
admin/dev_db_config.php

View File

@ -1,9 +1,14 @@
<?php
session_start();
?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="../styles/primary.css" />
<link rel="stylesheet" href="../styles/admin.css" /> <link rel="stylesheet" href="../styles/admin.css" />
<link rel="stylesheet" href="../styles/admin_nav.css" /> <link rel="stylesheet" href="../styles/admin_nav.css" />
<script src="../scripts/trojan.js"></script> <script src="../scripts/trojan.js"></script>
@ -11,6 +16,7 @@
</head> </head>
<body id="body"> <body id="body">
<script>getURL();</script>
<div id="contentFrame"> <div id="contentFrame">
<h1>Trojan's Trophy Room</h1> <h1>Trojan's Trophy Room</h1>
<h2 id="adminHeader">ADMIN PANEL</h2> <h2 id="adminHeader">ADMIN PANEL</h2>
@ -49,15 +55,26 @@
if ($count == 0) { // If no safe admins are found, we'll force creation of one if ($count == 0) { // If no safe admins are found, we'll force creation of one
echo "<iframe src=\"user_management/create_safe_admin.php\" name=\"dataFrame\" class=\"dataFrame\" id=\"dataFrame\" onload=\"resizeIframe(this);\"></iframe>"; echo "<iframe src=\"user_management/create_safe_admin.php\" name=\"dataFrame\" class=\"dataFrame\" id=\"dataFrame\" onload=\"resizeIframe(this);\"></iframe>";
} else { // Otherwise we'll show the nav page } else { // Otherwise we'll show the nav page
echo "<iframe src=\"admin_nav.php\" name=\"dataFrame\" class=\"dataFrame\" id=\"dataFrame\" onload=\"resizeIframe(this);\"></iframe>"; if (!isset($_SESSION["userID"])){
echo "<iframe src=\"../login_page.php?redirect=admin\" name=\"dataFrame\" class=\"dataFrame\" id=\"dataFrame\" onload=\"resizeIframe(this);\"></iframe>";
} else if (isset($_SESSION["userID"]) && $_SESSION["isAdmin"] == 1) {
echo "<iframe src=\"admin_nav.php\" name=\"dataFrame\" class=\"dataFrame\" id=\"dataFrame\" onload=\"resizeIframe(this);\"></iframe>";
} else {
echo "<iframe src=\"error.php\" name=\"dataFrame\" class=\"dataFrame\" id=\"dataFrame\" onload=\"resizeIframe(this);\"></iframe>";
}
} }
?> ?>
<div id="subNav"> <div id="subNav">
<a href="./" class="navLink" id="adminHomeButton">ADMIN HOME</a> <a href="./" class="navLink" id="adminHomeButton">ADMIN HOME</a>
<a href="../" class="navLink" id="mainHomeButton">MAIN HOME</a> <a href="../" class="navLink" id="mainHomeButton">MAIN HOME</a>
<p class="newLine"></p>
<?php
if (isset($_SESSION["userID"])){
echo "<a href=\"../logout.php?redirect=admin\" class=\"navLink\" id=\"logoutButton\">LOGOUT</a>";
}
?>
</div> </div>
</div> </div>
</body> </body>

View File

@ -26,7 +26,7 @@
// Variables for the various input fields // Variables for the various input fields
$username = $_POST["username"]; $username = $_POST["username"];
$password = password_hash($_POST["password"], PASSWORD_DEFAULT); // Hash the password for security $password = password_hash($_POST["password"], PASSWORD_DEFAULT); // Hash the password for security
$discord = ""; $discord = $_POST["discord"];
$twitch = $_POST["twitch"]; $twitch = $_POST["twitch"];
$youtube = $_POST["youtube"]; $youtube = $_POST["youtube"];
@ -36,11 +36,6 @@
$isAdmin = 1; $isAdmin = 1;
} }
echo "<p>Is Admin? " . $isAdmin . "</p>";
if (isset($_POST["discord"])) {
$discord = $_POST["discord"];
}
echo "<br>"; echo "<br>";
echo $username . "<br>"; echo $username . "<br>";

View File

@ -89,4 +89,14 @@ function verifyInput() {
alert ("Password must have a minimum length of 6 characters."); alert ("Password must have a minimum length of 6 characters.");
return false; return false;
} }
}
function displayPassword() {
// This will check to see if we want the password visible, and sets it as such
console.log("loaded usermanagement");
if (document.getElementById("showPassword").checked) {
document.getElementById("password").type = "text";
} else if (!(document.getElementById("showPassword").checked)) {
document.getElementById("password").type = "password";
}
} }

44
index.php Normal file
View File

@ -0,0 +1,44 @@
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="styles/primary.css" />
<script src="scripts/trojan.js"></script>
<title>Trojan's Trophy Room</title>
</head>
<body id="body">
<div id="contentFrame">
<h1>Trojan's Trophy Room</h1>
<h3>Choose a division to see results!</h3>
<div id="navPanel">
<a href="open.html" target="dataFrame" class="navLink">OPEN</a>
<a href="intermediate.html" target="dataFrame" class="navLink">INTERMEDIATE</a>
<a href="main.html" target="dataFrame" class="navLink">MAIN</a>
<p class="newLine"></p>
<a href="general.html" target="dataFrame" class="navLink">GENERAL (HOME)</a>
</div>
<p>&nbsp;</p>
<iframe src="open.html" name="dataFrame" class="dataFrame" id="dataFrame" onload="resizeIframe(this);"></iframe>
<p class="newLine"></p>
<p class="newLine"></p>
<div id="subNav">
<?php
if (isset($_SESSION["userID"])){
echo "<a href=\"logout.php \" class=\"navLink\" id=\"logoutButton\">LOGOUT</a>";
} else {
echo "<a href=\"login_page.php \" target=\"dataFrame\" class=\"navLink\" id=\"loginButton\">SIGN IN</a>";
}
?>
</div>
</div>
</body>
</html>

89
login.php Normal file
View File

@ -0,0 +1,89 @@
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="db_management.css" />
<link rel="stylesheet" href="login.css" />
<!-- <script src="trojan.js"></script>-->
<title>no title</title>
</head>
<body class="sqlOutput">
<?php
// USER-DEFINED VARIABLES
include("admin/db_config.php"); // Include database stuff
try { // Try opening the SQL database connection
$conn = new PDO("mysql:host=$servername; dbname=$dbName", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Get username and password out of the POST data
$username = $_POST["username"];
$password = $_POST["password"];
// THIS SHOULD BE MADE MORE EFFICIENT WITH ONLY ONE QUERY IF POSSIBLE
// Grab the password hash for the username (if available)
$sqlGetPasswordHash = $conn->prepare("SELECT password FROM " . $userTableName . " WHERE username=\"" . $username . "\"");
$sqlGetUserID = $conn->prepare("SELECT userID FROM " . $userTableName . " WHERE username=\"" . $username . "\"");
$sqlGetisAdmin = $conn->prepare("SELECT isAdmin FROM " . $userTableName . " WHERE username=\"" . $username . "\"");
$sqlGetPasswordHash->execute();
$sqlGetUserID->execute();
$sqlGetisAdmin->execute();
} catch (PDOException $e) { // failed connection
echo "Connection failed: " . $e->getMessage();
}
// Grab the hash from the fetched SQL data
$passwordHash = $sqlGetPasswordHash->fetchColumn();
$userID = $sqlGetUserID->fetchColumn();
$isAdmin = $sqlGetisAdmin->fetchColumn();
// Verify that the entered password matches the hashed one
if (password_verify($password, $passwordHash)) {
echo "<p>Welcome $username, please wait while we redirect you...</p>";
$_SESSION["userID"] = $userID;
$_SESSION["username"] = $username;
$_SESSION["isAdmin"] = $isAdmin;
// Function from StackOverflow used to get the base URL, to which we append
// the redirect (where the user came from)
function url(){
return sprintf(
"%s://%s/%s",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['SERVER_NAME'],
$_GET["redirect"]
);
}
$address = url();
echo "<p>$address</p>";
echo "<script>window.top.location.href = \"" . $address . "\";</script>";
} else {
echo "<p>Invalid credentials</p>";
}
// Close the SQL connection
$conn = null;
?>
</body>
</html>

39
login_page.php Normal file
View File

@ -0,0 +1,39 @@
<?php
session_start();
$redirect = $_GET["redirect"];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="styles/primary.css" />
<link rel="stylesheet" href="styles/login.css" />
<script src="admin/user_management/user_management.js"></script>
<title>ADMIN PANEL - Trojan's Trophy Room</title>
</head>
<body id="loginBody">
<h3 id="loginNotice">Sign in to continue</h3>
<div id="loginPanel">
<form id="loginForm" action="login.php?redirect=<?php echo $redirect; ?>" method="POST">
<div id="inputArea">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required>
<p class="newLine"></p>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required>
<p class="newLine"></p>
<label for="showPassword" id="showPasswordLabel">Show Password: &nbsp;</label>
<input type="checkbox" name="showPassword" id="showPassword" onchange="displayPassword();">
<p class="newLine">&nbsp;</p>
</div>
<div id="submitButton">
<input type="submit" value="Log In">
</div>
</form>
</div>
<p class="newLine"></p>
</body>
</html>

35
logout.php Normal file
View File

@ -0,0 +1,35 @@
<?php
session_start();
// Unset session variables
$_SESSION = array();
// Destory the cookie
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Destroy the session
session_destroy();
// Function from StackOverflow used to get the base URL, to which we append
// the redirect (where the user came from)
function url(){
return sprintf(
"%s://%s/%s",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['SERVER_NAME'],
$_GET["redirect"]
);
}
echo "
<script>window.location.href = \"" . url() . "\";</script>
";
?>

View File

@ -3,4 +3,12 @@ function resizeIframe(obj) {
obj.style.width = "100px"; obj.style.width = "100px";
obj.style.height = obj.contentWindow.document.documentElement.scrollHeight + 'px'; obj.style.height = obj.contentWindow.document.documentElement.scrollHeight + 'px';
obj.style.width = obj.contentWindow.document.documentElement.scrollWidth + 'px'; obj.style.width = obj.contentWindow.document.documentElement.scrollWidth + 'px';
}
function getURL(path) {
if (path == undefined) {
path = "";
}
console.log(window.location.href + path);
return window.location.href + path;
} }

View File

@ -1,36 +1,3 @@
#body {
background-image: linear-gradient(to right, rgba(0, 0, 255, .8), rgba(255, 165, 0, .8));
padding-top: 2%;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
height:100%
}
#contentFrame {
display: flex;
flex-direction: column;
margin: auto;
border: 1px solid black;
border-radius: 5px;
background-color: rgba(255, 255, 255, .2);
width:80%;
max-width: 900px;
min-height: 0px;
padding-top: 20px;
padding-left: 50px;
padding-right: 50px;
padding-bottom: 40px;
}
#contentFrame h1 {
margin: auto;
padding-bottom: 20px;
}
#contentFrame h2,h3 {
margin: auto;
padding-bottom: 30px;
}
#adminHeader { #adminHeader {
font-size: 200%; font-size: 200%;
} }
@ -61,13 +28,6 @@
transform: translateY(2px); transform: translateY(2px);
} }
#subNav {
display: flex;
flex-direction: row;
gap: 1%;
justify-content: center;
gap: 2%;
}
#mainHomeButton { #mainHomeButton {
box-shadow: 0px 2px 4px; box-shadow: 0px 2px 4px;
@ -80,4 +40,4 @@
#mainHomeButton:active { #mainHomeButton:active {
box-shadow: 0px 0px 2px; box-shadow: 0px 0px 2px;
transform: translateY(2px); transform: translateY(2px);
} }

91
styles/login.css Normal file
View File

@ -0,0 +1,91 @@
#loginBody {
width: 275px;
margin: 0;
}
#loginNotice {
font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
text-align: center;
}
#loginPanel {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
margin: auto;
border: 1px solid black;
border-radius: 4px;
padding: 10%;
padding-top: 8%;
padding-bottom: 12%;
width:100%;
}
#inputArea {
justify-content: left;
}
#loginForm {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
justify-content: center;
}
#loginForm input {
background-color: rgba(255, 255, 255, 0.6);
border-style: 1px solid blue;
}
#loginForm label {
font-weight: bold;
}
#loginForm input[type="submit"] {
justify-content: center;
margin: auto;
padding: 5px 15px;
font-size: 100%;
font-weight: bold;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: rgba(255, 255, 255, 0);
border-radius: 6px;
border: 1px solid blue;
box-shadow: 0px 2px 4px;
}
#loginForm input[type="submit"]:hover {
color: black;
background-color: rgba(255, 165, 0, .6);
}
#loginForm input[type="submit"]:active {
box-shadow: 0px 0px 2px;
transform: translateY(2px);
}
#loginForm input[type="text"] {
border-radius: 2px;
width: 75%;
padding: 7px 5%;
margin: 1% 6%;
}
#loginForm input[type="password"] {
border-radius: 2px;
width: 75%;
padding: 7px 5%;
margin: 1% 6%;
}
#showPasswordLabel label {
font-weight: lighter;
justify-content: flex-end;
}
#submitButton {
display: flex;
justify-content: center;
margin:auto;
align-items: center;
}

View File

@ -26,7 +26,7 @@
padding-bottom: 20px; padding-bottom: 20px;
} }
#contentFrame h3 { #contentFrame h2,h3 {
margin: auto; margin: auto;
padding-bottom: 30px; padding-bottom: 30px;
} }
@ -68,12 +68,47 @@
margin: auto; margin: auto;
padding: 0; padding: 0;
border: none; border: none;
border-radius: 5px; /*border-radius: 5px;
box-shadow: 0px 10px 15px; box-shadow: 0px 10px 15px;
background-color: rgba(183, 183, 255, 0.6); background-color: rgba(183, 183, 255, 0.6);*/
max-width: 90%; max-width: 90%;
} }
.newLine { .newLine {
width: 100%; width: 100%;
} }
#subNav {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
gap: 2%;
}
#logoutButton {
box-shadow: 0px 2px 4px;
font-weight: bold;
}
#logoutButton:hover {
color: black;
background-color: rgba(255, 165, 0, .6);
}
#logoutButton:active {
box-shadow: 0px 0px 2px;
transform: translateY(2px);
}
#loginButton {
box-shadow: 0px 2px 4px;
font-weight: bold;
}
#loginButton:hover {
color: black;
background-color: rgba(255, 165, 0, .6);
}
#loginButton:active {
box-shadow: 0px 0px 2px;
transform: translateY(2px);
}