From d36d353ce2ad6605ff4d92a04264086dc081bb58 Mon Sep 17 00:00:00 2001
From: Eirik Th S
Date: Mon, 22 Nov 2021 16:33:46 +0100
Subject: [PATCH] Modul 7
---
modul1/index.php | 2 +-
modul7/Auth.inc.php | 97 +++++++++++++++++++++++++++++++++++++++++++++
modul7/Hjem.php | 26 ++++++++++++
modul7/index.php | 1 +
modul7/login.php | 65 ++++++++++++++++++++++++++++++
5 files changed, 190 insertions(+), 1 deletion(-)
create mode 100644 modul7/Auth.inc.php
create mode 100644 modul7/Hjem.php
create mode 120000 modul7/index.php
create mode 100644 modul7/login.php
diff --git a/modul1/index.php b/modul1/index.php
index ad717ca..e9295b5 100644
--- a/modul1/index.php
+++ b/modul1/index.php
@@ -28,7 +28,7 @@ function getModuleName($input): string {
".getModuleName($item)."
\n";
}
}
diff --git a/modul7/Auth.inc.php b/modul7/Auth.inc.php
new file mode 100644
index 0000000..168a194
--- /dev/null
+++ b/modul7/Auth.inc.php
@@ -0,0 +1,97 @@
+passord)){
+ // valid login
+ echo "VALID";
+
+ // Start en session
+ session_start();
+
+ // Lagre id og epost i en SESSION-variabeler
+ $_SESSION['id'] = $bruker->dbid;
+ $_SESSION['brukernavn'] = $bruker->epost;
+
+ // Eksta sikkerhet. Hindrer at noen kan ta cookien fra noens nettleser og bruke i en annen nettleser. (versjon må matche 100% for å være gyldig)
+ $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);
+ }
+ else {
+ throw new LoginException("Feil brukernavn og/eller passord. Prøv igjen eller kontakt administrator.");
+ }
+ }
+ catch (PDOException $e){
+ throw new LoginException("SQL-feil: ".$e);
+ }
+ }
+}
+
+class LoginException extends RuntimeException {
+ public function __construct($message = "", $code = 0, Throwable $previous = null){
+ parent::__construct($message, $code, $previous);
+ }
+}
+
+class Bruker {
+ public int $dbid;
+ public string $epost;
+ public string $passord;
+
+ public static function hentFraEpost(string $epost): ?Bruker{
+ $hentBrukerSQL = "SELECT * FROM Bruker WHERE epost = ?";
+
+ $db = getPdoConn(); // DATABASE-tilkobling
+
+ $stmt = $db->prepare($hentBrukerSQL);
+ $stmt->bindValue(1, $epost);
+ $stmt->execute();
+
+ $stmt->setFetchMode(PDO::FETCH_ASSOC);
+
+ if($stmt->rowCount() != 1){
+ return null;
+ }
+
+ foreach($stmt->fetchAll() as $row){
+ $bruker = new Bruker();
+ $bruker->dbid = $row['brukerId'];
+ $bruker->epost = $row['epost'];
+ $bruker->passord = $row['passord'];
+
+ return $bruker;
+ }
+ $db = null;
+
+ return null;
+ }
+}
\ No newline at end of file
diff --git a/modul7/Hjem.php b/modul7/Hjem.php
new file mode 100644
index 0000000..5ea14c8
--- /dev/null
+++ b/modul7/Hjem.php
@@ -0,0 +1,26 @@
+
+
+
+ =title();?>
+
+
+
+
+
+
=title();?>
+
=getNavigation();?>
+
Autentisering
+
+
Hjem-siden
+
Kun innloggede har tilgang her!
+
+
+
+
\ No newline at end of file
diff --git a/modul7/index.php b/modul7/index.php
new file mode 120000
index 0000000..85750bf
--- /dev/null
+++ b/modul7/index.php
@@ -0,0 +1 @@
+../modul1/index.php
\ No newline at end of file
diff --git a/modul7/login.php b/modul7/login.php
new file mode 100644
index 0000000..3b38865
--- /dev/null
+++ b/modul7/login.php
@@ -0,0 +1,65 @@
+getMessage();
+ }
+}
+?>
+
+
+ =title();?>
+
+
+
+
+
+
Modul 7 - Autentisering
+
=getNavigation();?>
+
Login
+
+ ".$e."\n";
+ }
+ }
+
+ if(!empty($msg)){
+ foreach ($msg as $m){
+ echo "
".$m."
\n";
+ }
+ }
+
+ ?>
+
+
+
+
+
+
\ No newline at end of file