Last active
August 29, 2015 13:56
-
-
Save ntpz/9018200 to your computer and use it in GitHub Desktop.
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
<? | |
session_start(); | |
function login($username, $password) { | |
if($password == '53cr3t') { | |
return array('username' => $username); | |
} else { | |
return null; | |
} | |
} | |
$message = ''; | |
if(isset($_GET['logout'])) { | |
unset($_SESSION['user']); | |
$message = 'Logged out'; | |
} elseif(isset($_REQUEST['username']) && isset($_REQUEST['password'])) { | |
$user = login($_REQUEST['username'], $_REQUEST['password']); | |
if($user) { | |
$_SESSION['user'] = $user; | |
$message = 'Logged in'; | |
} else { | |
$message = 'Invalid username or password!'; | |
} | |
} | |
?><!DOCTYPE html> | |
<html> | |
<head> | |
<title>Login!</title> | |
</head> | |
<body> | |
<p><?=$message?></p> | |
<? if(!isset($_SESSION['user'])) : ?> | |
<form> | |
<input name=username placeholder=Username> | |
<input type=password name=password placeholder=Password> | |
<button type=submit>Login</button> | |
</form> | |
<? else: ?> | |
Welcome, <strong><?=$_SESSION['user']['username']?></strong>!<br> | |
<a href=?logout=true>logout</a> | |
<? endif; ?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment