Created
August 16, 2014 01:59
-
-
Save kyfreed/45af335d554a88f4929f to your computer and use it in GitHub Desktop.
simple login
This file contains hidden or 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 | |
include 'accountfunctions.php'; | |
requireUser('t1'); |
This file contains hidden or 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 | |
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'); | |
} |
This file contains hidden or 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 | |
$accounts = array( | |
'jim' => user('password', 't1'), | |
'eric' => user('smile', 't2'), | |
); |
This file contains hidden or 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 | |
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