PHP

SIMPLE LOGIN SYSTEM IN PHP WITHOUT DATABASE

(a quick example)

HTML LOGIN FORM <form method="post">   <input type="text" name="user">   <input type="password" name="pass"   <input type="submit" value="Log In"> </form>

HTML LOGIN FORM

01

PROCESS LOGIN ON FORM SUBMIT <?php require "LOGIN.PHP"; ?>

START SESSION session_start();

PHP LOGIN PROCESS (A)

02

if (isset($_POST["user"]) && !isset($_SESSION["user"])) {   USERS LIST   $usr = ["joe" => "123456"];     CHECK & VERIFY   if (isset($usr[$_POST["user"]]) &&    $usr[$_POST["user"]]==$_POST["pass"])   { $_SESSION["user"] = $_POST["user"]; } }

PHP LOGIN PROCESS (B)

03

REDIRECT TO HOME PAGE ON LOGIN if (isset($_SESSION["user"])) {    header("Location: index.php");   exit(); }

ON TOP OF ALL PAGES -  CHECK IF USER SIGNED IN if (!isset($_SESSION["user"])) {    header("Location: login.php");   exit(); }

PROTECT ALL PAGES

04

LOGOUT FORM <form method="post">   <input type="hidden" name="logout"   value="1">   <input type="submit" value="Logout"> </form>

LOGGING OFF

05

PHP END SESSION if (isset($_POST["logout"])) {    unset($_SESSION);   session_destroy(); }