Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active September 27, 2023 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save code-boxx/945ed4526453ca73d82f63272cd2c346 to your computer and use it in GitHub Desktop.
Save code-boxx/945ed4526453ca73d82f63272cd2c346 to your computer and use it in GitHub Desktop.
PHP User Login Without Database

PHP USER LOGIN WITHOUT DATABASE

https://code-boxx.com/simple-php-login-without-database/

NOTES

  1. Set your own users and passwords in $users of 2-check.php, also where to redirect on successful login.
  2. Protect all your pages by including 3-protect.php at the top.
  3. Launch 1-login.php in the web browser, that's all.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* (A) WHOLE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
body {
max-width: 500px;
margin: 0 auto;
padding: 20px;
background: #f7f7f7;
}
/* (B) LOGIN FORM */
#login-form {
padding: 20px;
border: 1px solid #ebebeb;
background: #fff;
}
#login-form h1 {
font-size: 1.5em;
margin: 0 0 20px 0;
}
#login-form label, #login-form input {
display: block;
width: 100%;
margin-top: 10px;
}
#login-form label { color: #767676; }
#login-form input {
padding: 10px;
border: 1px solid #adadad;
}
#login-form input[type=submit] {
margin-top: 20px;
border: 0;
color: #fff;
background: #a52323;
cursor: pointer;
}
/* (C) INVALID LOGIN */
#login-bad {
padding : 10px;
margin-bottom: 20px;
background: #ffe7e7;
border: 1px solid #ff3e3e;
color: #c10000;
font-weight: bold;
}
<?php
// (A) LOGIN CHECKS
require "2-check.php";
// (B) LOGIN PAGE HTML ?>
<!DOCTYPE html>
<html>
<head>
<title>Login Page Demo</title>
<link href="1-login.css" rel="stylesheet">
</head>
<body>
<?php if (isset($failed)) { ?>
<div id="login-bad">Invalid email or password.</div>
<?php } ?>
<form id="login-form" method="post" target="_self">
<h1>PLEASE SIGN IN</h1>
<label for="user">User</label>
<input type="text" name="user" required>
<label for="password">Password</label>
<input type="password" name="password" required>
<input type="submit" value="Sign In">
</form>
</body>
</html>
<?php
// (A) START SESSION
session_start();
// (B) PROCESS LOGIN
if (isset($_POST["user"]) && !isset($_SESSION["user"])) {
// (B1) USERS & PASSWORDS - SET YOUR OWN !
$users = [
"joe" => "123456",
"jon" => "654321",
"joy" => "987654"
];
// (B2) CHECK & VERIFY
if (isset($users[$_POST["user"]]) && $users[$_POST["user"]] == $_POST["password"]) {
$_SESSION["user"] = $_POST["user"];
}
// (B3) FAILED LOGIN FLAG
if (!isset($_SESSION["user"])) { $failed = true; }
}
// (C) REDIRECT TO HOME PAGE IF SIGNED IN - SET YOUR OWN !
if (isset($_SESSION["user"])) {
header("Location: 4-dummy.php");
exit();
}
<?php
// (A) START SESSION
session_start();
// (B) LOGOUT REQUEST
if (isset($_POST["logout"])) {
session_destroy();
unset($_SESSION);
}
// (C) REDIRECT TO LOGIN PAGE IF NOT SIGNED IN
if (!isset($_SESSION["user"])) {
header("Location: 1-login.php");
exit();
}
<?php
// (A) THIS IS A PROTECTED DUMMY PAGE
require "3-protect.php";
// (B) HTML AS USUAL ?>
<!DOCTYPE html>
<html>
<head>
<title>Demo Page</title>
</head>
<body>
<!-- (B1) LOGOUT FORM -->
<form method="post">
<input type="hidden" name="logout" value="1">
<input type="submit" value="Sign Out">
</form>
<!-- (B2) WHO IS THE CURRENT USER? -->
<?=$_SESSION["user"]?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment