Skip to content

Instantly share code, notes, and snippets.

@theoremoon
Created April 27, 2017 08:19
Show Gist options
  • Select an option

  • Save theoremoon/5d49f643b71ccde13998c7ed37a4c1df to your computer and use it in GitHub Desktop.

Select an option

Save theoremoon/5d49f643b71ccde13998c7ed37a4c1df to your computer and use it in GitHub Desktop.
はるかむかしのやつがのこってたので
<?php
define('JSONFILE', 'your.json');
function isLogin() {
return isset($_SESSION['you']);
}
function h($s) {
return htmlspecialchars($s, ENT_QUOTES);
}
function numberofyou() {
$json = load();
return count($json);
}
function yourname() {
if (isLogin()) {
return $_SESSION['you'];
}
return "";
}
function yourrecord() {
return load()[yourname()]['record'];
}
function yourorder() {
$json = load();
$all = [];
foreach ($json as $k => $v) {
$all []= $v['record'];
}
rsort($all, SORT_NUMERIC);
return (array_search($json[yourname()]['record'], $all) + 1) . " / " . count($all);
}
function load() {
return json_decode(file_get_contents(JSONFILE), true);
}
function save($data) {
return file_put_contents(JSONFILE, json_encode($data));
}
function login($username, $password) {
$json = load();
if (isset($json[$username]) && $json[$username]["password"] === sha1($password)) {
$_SESSION['you'] = $username;
return true;
}
throw new Exception("Failed to login. Incorrect username and password.");
}
function register($username, $password) {
if (count($username) <= 0 || count($password) <= 0) {
throw new Exception("Username or password is invalid.");
}
$json = load();
if (isset($json[$username])) {
throw new Exception("Username is already used.");
}
$json[$username] = [
"password" => sha1($password),
"record" => 0
];
save($json);
login($username, $password);
}
function update($record) {
if (! is_numeric($record)) {
throw new Exception("Invalid record");
}
if (isLogin()) {
$json = load();
$json[yourname()]['record'] = $record;
save($json);
}
}
$error = null;
session_start();
try {
if (isset($_GET['action'])) {
unset($_SESSION['you']);
} else if (isset($_POST['action'])) {
if ($_POST['action'] === 'Login') {
login($_POST['username'], $_POST['password']);
}
if ($_POST['action'] === 'Register') {
register($_POST['username'], $_POST['password']);
}
if ($_POST['action'] === 'Update') {
update($_POST['record']);
}
}
} catch(Exception $e) {
$error = $e->getMessage();
}
?>
<header>
<div class="topbar">
<div class="topbar-item">
<?php if (isLogin()) { ?>
<div>
<span><?php echo h(yourname()); ?></span>
<a href="order.php?action=Logout">Logout</a>
</div>
<?php } else { ?>
<form action="order.php" method="post" class="topbar-form">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login" name="action">
<input type="submit" value="Register" name="action">
</form>
<?php } ?>
</div>
</div>
</div>
<h1>君の順位は。 -- Your Order.</h1>
<?php if($error) { echo h($error); } ?>
</header>
<?php if (! isLogin()) { ?>
<main>
<div class="numberofyou">
<?php echo h(numberofyou()); ?> users are here now.
</div>
<form action="order.php" method="post" class="registeryou">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Register" name="action">
</form>
</main>
<?php exit(); ?>
<?php } ?>
<main>
<div class="numberofyou">
<?php echo h(numberofyou()); ?> users are here now.
</div>
<div class="yourorder">
Your order is <?php echo h(yourorder()); ?>
</div>
<form class="yourrecord" method="post" class="updaterecord">
Your record is <input type="text" value="<?php echo h(yourrecord()); ?>" name="record">
<input type="submit" value="Update" name="action">
</form>
</main>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment