Created
September 29, 2017 13:51
-
-
Save makzan/e260dfffd60ac6ef9192301f29be5f04 to your computer and use it in GitHub Desktop.
Basic PHP hardcoded login/logout
This file contains 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_id("thomas23"); | |
session_start(); | |
// initialize the login name session variable. | |
$_SESSION["login_name"] = $_SESSION["login_name"] ?? ""; | |
// there is data POST from user. | |
if (count($_POST) > 0) { | |
// logout or login? | |
if (isset($_POST["logout"])) { | |
$_SESSION["login_name"] = ""; | |
} else if ($_POST["username"] == "thomas" && $_POST["password"] == "1234") { | |
$_SESSION["login_name"] = "thomas"; | |
} | |
} | |
$is_logged_in = ($_SESSION["login_name"] != ""); | |
?> | |
<html> | |
<?php if ($is_logged_in): ?> | |
<h1> | |
Welcome <?= $_SESSION["login_name"] ?>! | |
</h1> | |
<form action="" method="POST"> | |
<input type="submit" name="logout" value="Logout"> | |
</form> | |
<?php else: ?> | |
<form action="" method="POST"> | |
<p> | |
Username<br> | |
<input type="text" name="username"> | |
</p> | |
<p> | |
Password<br> | |
<input type="password" name="password"> | |
</p> | |
<input type="submit" value="Login"> | |
</form> | |
<?php endif ?> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment