Web Programming with PHP

18 Users Validation against an Array

Usernames and passwords are stored in an array in the check-login web page.

Action page of login.php:

login_action.php

<?php
// Array of users: username => password
$users["admin"] = "adminadmin";
$users["vadim"] = "vadimvadim";

	
foreach($users as $name => $password) {
  if($name == $_POST["username"] && $password == $_POST["password"]) {
    // Username and password are correct
	header("Location: private.php");
	die();
  }		
}	

// Username and password are incorrect
header("Location: login.php")
?>