Modul 7
parent
8a977a019f
commit
d36d353ce2
|
@ -28,7 +28,7 @@ function getModuleName($input): string {
|
|||
|
||||
<?php
|
||||
foreach($dir as $item){
|
||||
if(stristr($item, '.php') || stristr($item, '.pdf')){
|
||||
if((stristr($item, '.php') || stristr($item, '.pdf')) && !stristr($item, '.inc.php')){
|
||||
echo "<p><a href='$item'>".getModuleName($item)."</a></p>\n";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
class Auth {
|
||||
private bool $isLoggedIn = false;
|
||||
|
||||
public static function checkLogin(): bool {
|
||||
// Start SESSION hvis den ikke alt er startet.
|
||||
if(!isset($_SESSION)){
|
||||
session_start();
|
||||
}
|
||||
|
||||
return isset($_SESSION['id'], $_SESSION['brukernavn'], $_SESSION['agent']) && $_SESSION['agent'] === md5($_SERVER['HTTP_USER_AGENT']);
|
||||
}
|
||||
|
||||
|
||||
public static function requireLogin(){
|
||||
if(!self::checkLogin()){
|
||||
header("Location: /modul7/login.php");
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LoginException
|
||||
*/
|
||||
public static function doLogin(string $brukernavn, string $passord){
|
||||
if(empty($brukernavn) || empty($passord)){
|
||||
throw new LoginException("Ugyldige input-felt"); // Vag beskrivelse da disse vil bli verifisert først av HTML5
|
||||
}
|
||||
|
||||
try {
|
||||
// Hent bruker fra database med passende epost
|
||||
$bruker = Bruker::hentFraEpost($brukernavn);
|
||||
|
||||
if($bruker !== null && password_verify($passord, $bruker->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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
include '../common.php';
|
||||
include '../modul6/forberedelser.php';
|
||||
include 'Auth.inc.php';
|
||||
Auth::requireLogin();
|
||||
|
||||
//echo password_hash("Passord1234", PASSWORD_DEFAULT);
|
||||
?><!DOCTYPE html>
|
||||
<html lang="no">
|
||||
<head>
|
||||
<title><?=title();?></title>
|
||||
<link rel="stylesheet" href="../index.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
<h1><?=title();?></h1>
|
||||
<h4><?=getNavigation();?></h4>
|
||||
<h2>Autentisering</h2>
|
||||
|
||||
<h3>Hjem-siden</h3>
|
||||
<p>Kun innloggede har tilgang her!</p>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1 @@
|
|||
../modul1/index.php
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
include '../common.php';
|
||||
// Bruker database-tilkobling fra forrige modul
|
||||
include '../modul6/forberedelser.php';
|
||||
include "Auth.inc.php";
|
||||
|
||||
$msg = array();
|
||||
$err = array();
|
||||
|
||||
if(!empty($_POST)){
|
||||
try {
|
||||
Auth::doLogin($_POST['brukernavn'], $_POST['passord']);
|
||||
header("Location: Hjem.php");
|
||||
}
|
||||
catch (LoginException $e){
|
||||
$err[] = $e->getMessage();
|
||||
}
|
||||
}
|
||||
?><!DOCTYPE html>
|
||||
<html lang="no">
|
||||
<head>
|
||||
<title><?=title();?></title>
|
||||
<link rel="stylesheet" href="../index.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
<h1>Modul 7 - Autentisering</h1>
|
||||
<h4><?=getNavigation();?></h4>
|
||||
<h2>Login</h2>
|
||||
|
||||
<?php
|
||||
// Her skriver vi ut feilmeldinger eller suksess-meldinger som er generert. Dette for å få et finere brukergrensesnitt.
|
||||
|
||||
if(!empty($err)){
|
||||
foreach ($err as $e){
|
||||
echo "<p class='error'>".$e."</p>\n";
|
||||
}
|
||||
}
|
||||
|
||||
if(!empty($msg)){
|
||||
foreach ($msg as $m){
|
||||
echo "<p class='suksess'>".$m."</p>\n";
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<form method="post">
|
||||
<p>
|
||||
<label for="brukernavn">Brukernavn:</label>
|
||||
<input type="text" id="brukernavn" name="brukernavn">
|
||||
</p>
|
||||
<p>
|
||||
<label for="passord">Passord:</label>
|
||||
<input type="password" id="passord" name="passord">
|
||||
</p>
|
||||
<p>
|
||||
<input type="submit" value="Logg inn...">
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue