Skip to content

Instantly share code, notes, and snippets.

@kyfreed
Created August 16, 2014 01:59
Show Gist options
  • Save kyfreed/45af335d554a88f4929f to your computer and use it in GitHub Desktop.
Save kyfreed/45af335d554a88f4929f to your computer and use it in GitHub Desktop.
simple login
<?php
include 'accountfunctions.php';
requireUser('t1');
<?php
function user($password, $tier) {
return array($password, $tier);
}
include 'accountlogins.php';
function getUser(){
global $accounts;
if (empty($_COOKIE["login"])){
return false;
}
list($username, $encryptedPassword) = explode(',' , $_COOKIE["login"]);
if(empty($accounts[$username])){
return false;
}
if(encryptPassword($accounts[$username][0]) == $encryptedPassword){
return $accounts[$username];
}
return false;
}
function requireUser($tier) {
$user = getUser();
if ($user == false){
header("Location: login.php");
die();
}
if ($user[1] != $tier){
header("Location: account-". $user[1] . ".php");
die();
}
}
function loginUser($username, $password){
global $accounts;
if(empty($accounts[$username])) {
return 'Username "'.$username.'" not found. Try again.';
}
if($accounts[$username][0] == $password){
setcookie('login', $username . ',' . encryptPassword($password));
header("Location: account-" . $accounts[$username][1] . ".php");
die();
}
return 'Invalid password. Try again.';
}
function encryptPassword($password) {
return hash('md5', $password . 'TLSrocks');
}
<?php
$accounts = array(
'jim' => user('password', 't1'),
'eric' => user('smile', 't2'),
);
<?php
include 'accountfunctions.php';
$msg = '';
if (isset($_POST['uname']) && isset($_POST['pwd'])){
$msg = loginUser($_POST['uname'],$_POST['pwd']);
} else if (isset($_GET['logout'])){
setcookie("login", "", time()-3600);
$msg = "Goodbye.";
}
?>
<?=$msg?>
<form action="login.php" method="post">
Username: <input type="text" name="uname"><br>
Password: <input type="password" name="pwd"><br>
<input type="submit" value="Submit">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment