Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chrisdiana
Last active September 10, 2021 12:24
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdiana/ac64daf20d405b33d7a6 to your computer and use it in GitHub Desktop.
Save chrisdiana/ac64daf20d405b33d7a6 to your computer and use it in GitHub Desktop.
PHP Login without Database
<?php
session_start();
// ***************************************** //
// ********** DECLARE VARIABLES ********** //
// ***************************************** //
$username = 'username';
$password = 'password';
$random1 = 'secret_key1';
$random2 = 'secret_key2';
$hash = md5($random1.$pass.$random2);
$self = $_SERVER['REQUEST_URI'];
// ************************************ //
// ********** USER LOGOUT ********** //
// ************************************ //
if(isset($_GET['logout'])) {
unset($_SESSION['login']);
}
// ******************************************* //
// ********** USER IS LOGGED IN ********** //
// ******************************************* //
if (isset($_SESSION['login']) && $_SESSION['login'] == $hash) {
?>
<p>Hello <?php echo $username; ?>, you have successfully logged in!</p>
<a href="?logout=true">Logout?</a>
<?php
}
// *********************************************** //
// ********** FORM HAS BEEN SUBMITTED ********** //
// *********************************************** //
else if (isset($_POST['submit'])) {
if ($_POST['username'] == $username && $_POST['password'] == $password){
//IF USERNAME AND PASSWORD ARE CORRECT SET THE LOG-IN SESSION
$_SESSION["login"] = $hash;
header("Location: $_SERVER[PHP_SELF]");
} else {
// DISPLAY FORM WITH ERROR
display_login_form();
echo '<p>Username or password is invalid</p>';
}
}
// *********************************************** //
// ********** SHOW THE LOG-IN FORM ********** //
// *********************************************** //
else {
display_login_form();
}
function display_login_form() { ?>
<form action="<?php echo $self; ?>" method='post'>
<label for="username">username</label>
<input type="text" name="username" id="username">
<label for="password">password</label>
<input type="password" name="password" id="password">
<input type="submit" name="submit" value="submit">
</form>
<?php } ?>
@Anasboulbali
Copy link

thank you

@kicktv
Copy link

kicktv commented Sep 9, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment