Created
February 18, 2016 17:11
-
-
Save thagxt/94b976db4c8f14ec1527 to your computer and use it in GitHub Desktop.
simple PHP login without a database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 } ?> |
use a web hosting service like gearhost.com or 000webhost.com or use WAMP for windows or XAMPP for linux to see php files in action
$pass = undefined variable
thanks very much.
the script works
I use win IIS express to run them
this script is for 6 years ago. wow.
idk why but I feel sad :(
I think it is very good
but I don't know ho to add a users.
Please tell me. Thanks.
Please sorry my English I'm from Czech
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
anyone please tell me how to open these php files. :)