Skip to content

Instantly share code, notes, and snippets.

@CuzImBisonratte
Last active January 6, 2022 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CuzImBisonratte/a31289d660d52ecb2081c6760f67ed26 to your computer and use it in GitHub Desktop.
Save CuzImBisonratte/a31289d660d52ecb2081c6760f67ed26 to your computer and use it in GitHub Desktop.
Login Skript
{
"testaccount": "password",
"testaccount2": "password2"
}
<div id="login">
<!-- Header -->
<h1>Login</h1>
<!-- The form -->
<form action="login.php" method="post">
<!-- Username field -->
<label for="username">
<i class="fas fa-user"></i>
</label>
<input type="text" name="username" placeholder="Nutzername" id="username" required>
<!-- Password field -->
<label for="password">
<i class="fas fa-lock"></i>
</label>
<input type="password" name="password" placeholder="Passwort" id="password" required>
<!-- Login-knopf -->
<input type="submit" value="Login">
</form>
</div>
<?php
// Check if the user is logged in
// If so, redirect to ../index.php
if (isset($_SESSION['loggedin'])) {
header('Location: ../index.php');
exit;
}
// Start sessions
session_start();
// Include the accounts.json file
$accounts = file_get_contents('accounts.json');
// Decode the accounts.json file
$accounts = json_decode($accounts, true);
// Get $_POST data
// There are $username and $password
$username = $_POST['username'];
$password = $_POST['password'];
// Go through the accounts.json file
// Check if the username and password match
foreach ($accounts as $account) {
if ($account['username'] == $username && $account['password'] == $password) {
// If so, set the loggedin session variable
$_SESSION['loggedin'] = true;
// And redirect to ../index.php
header('Location: ../index.php');
exit;
} else {
// Reload the login.html page
header('Location: index.html');
exit;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment