diff --git a/Assignments/Assignment 3.pdf b/Assignments/Assignment 3.pdf new file mode 100644 index 0000000000000000000000000000000000000000..42b61b60a9faaa1af16d566c3d0adee3788b3351 Binary files /dev/null and b/Assignments/Assignment 3.pdf differ diff --git a/php/Login.php b/php/Login.php index 9b093fd905222179c3579a5a8d21da0ab6f4f40c..5775bfd2baa5528be78628d0116173895263c86b 100644 --- a/php/Login.php +++ b/php/Login.php @@ -63,14 +63,50 @@ EOT; if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST[self::USERNAME]) && isset($_POST[self::PASSWORD])) { - $_SESSION[EXPIRES] = time(); + $username = $_POST[self::USERNAME]; + $password = $_POST[self::PASSWORD]; - header('Location: Admin.php'); - die(); + $isValid = $this->checkValidityOfCredentials($username, $password); + + if (!$isValid) { + $this->alert('Username or password is wrong!'); + } else { + $_SESSION[EXPIRES] = time(); + + header('Location: Admin.php'); + die(); + } } } } + private function checkValidityOfCredentials(string $username, string $password):bool { + $isValid = false; + + $password = hash("sha3-256", $password); + + // Alternative to prepared statements is real_escape_string function on mysqli object + $stmt = $this->database->prepare("SELECT * FROM users u WHERE u.username = ?"); + $stmt->bind_param("s", $username); + $stmt->execute(); + $resultSet = $stmt->get_result(); + + if ($row = $resultSet->fetch_assoc()) { + $hashedPassword = $row[self::PASSWORD]; + + $isValid = $password == $hashedPassword; + } + + $resultSet->free(); + $stmt->close(); + + return $isValid; + } + + private function alert(string $message):void { + echo "<script>alert('$message');</script>"; + } + public static function main():void { try { diff --git a/php/Page.php b/php/Page.php index 273d2be48c0c74fa9c37a4e5bd5f27ce3ac3dd4b..786ccda31d8bc56dd48f081808bdcba0eff39208 100644 --- a/php/Page.php +++ b/php/Page.php @@ -5,20 +5,9 @@ abstract class Page { protected mysqli $database; protected function __construct() { - return; - error_reporting(E_ALL); - $host = "localhost"; - - /********************************************/ - // This code switches from the the local installation (XAMPP) to the docker installation - if (gethostbyname('mariadb') != "mariadb") { // mariadb is known? - $host = "mariadb"; - } - /********************************************/ - - $this->database = new mysqli("localhost","my_user","my_password","my_db"); + $this->database = new mysqli("localhost","root","","nasa"); // Check connection if ($this->database->connect_errno) { @@ -33,7 +22,7 @@ abstract class Page { } protected function __destruct() { - + /*$this->database->close();*/ } protected function generatePageHeader(string $title = ""):void