Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active August 29, 2015 14:25
Show Gist options
  • Save masakielastic/aea5fdeeff9167c54a73 to your computer and use it in GitHub Desktop.
Save masakielastic/aea5fdeeff9167c54a73 to your computer and use it in GitHub Desktop.
1つのファイルでログイン・ログアウトの練習
<?php
session_start();
$login = isset($_POST['username'])
&& $_POST['username'] === 'myuser'
&& isset($_POST['password'])
&& $_POST['password'] === 'mypass';
$logout = isset($_POST['logout'])
&& $_POST['logout'] === 'logout';
if ($login) {
$_SESSION['user'] = $_POST['username'];
} else if ($logout) {
unset($_SESSION['user']);
}
if (empty($_SESSION['user'])) {
?>
<form action="/test/index.php" method="POST">
<p>
<label for="username">ユーザー名</label>
<input type="text" name="username" />
</p>
<p>
<label for="password">パスワード</label>
<input type="password" name="password" />
</p>
<input type="submit" value="ログインする"/>
</form>
<?php
} else {
?>
<p>こんにちは、<?= $_SESSION['user'] ?>さん。</p>
<form action="/test/index.php" method="POST">
<input name="logout" type="hidden" value="logout" />
<input type="submit" value="ログアウトする" />
</form>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment