need help understand how this PHP login array code works?

  arrays, html, php

So the code at the bottom is an example code from my class of how to do a login page, it takes what is posted in an input box from an html file and receives this in a php file, the code then checks the username and password from an array and verify it or declines it, I can’t understand what the syntax means in the if(), specifically the "isset($_POST[‘user’]) && !isset($_SESSION[‘user’])" and such, I was hoping someone could break down what exactly each line does or what the syntax means? thanks for your time.

// (A) START SESSION
session_start();

// (B) HANDLE 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']])) {
    if ($users[$_POST['user']] == $_POST['password']) {
      $_SESSION['user'] = $_POST['user'];
    }
  }

  // (B3) FAILED LOGIN FLAG
  if (!isset($_SESSION['user'])) { $failed = true; }
}

Source: Ask PHP

LEAVE A COMMENT