Last active
August 29, 2015 14:25
-
-
Save masakielastic/aea5fdeeff9167c54a73 to your computer and use it in GitHub Desktop.
1つのファイルでログイン・ログアウトの練習
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 | |
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