Skip to content

Instantly share code, notes, and snippets.

@ntpz
Last active August 29, 2015 13:56
Show Gist options
  • Save ntpz/9018200 to your computer and use it in GitHub Desktop.
Save ntpz/9018200 to your computer and use it in GitHub Desktop.
<?
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