Källkod
Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.
webbserverprogrammering/exercises/mysql/exercise_2/
11 filer
admin.php
back_button.php
create_table.php
dbconnection.php
delete_row.php
index.php
insert_default_values.php
insert_values.php
print_table.php
start.php
update_values.php
back_button.php
create_table.php
dbconnection.php
delete_row.php
index.php
insert_default_values.php
insert_values.php
print_table.php
start.php
update_values.php
index.php
97 lines UTF-8 Windows (CRLF)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
<?php
error_reporting(-1); // Report all type of errors
ini_set('display_errors', 1); // Display all errors
ini_set('output_buffering', 0); // Do not buffer outputs, write directly
$message = "";
if (isset($_POST["submitted"]) && !empty($_POST["username"]) && !empty($_POST["password"])) {
$inputUsername = $_POST["username"];
$inputPassword = $_POST["password"];
include ('dbconnection.php');
try {
$sql = "SELECT * FROM users WHERE username=?";
$stmt = $dbconn->prepare($sql);
$data = [$inputUsername];
$stmt->execute($data);
$response = $stmt->fetch(PDO::FETCH_ASSOC);
if ($response["password"] === $inputPassword) {
if ($response["role"] === "admin") {
header("Location:admin.php");
} else if ($response["role"] === "regular") {
header("Location:start.php");
} else {
$message = "Error: Invalid user rights.";
}
} else {
$message = "Incorrect login credentials.";
}
}
catch(PDOException $e) {
$message .= $sql . "<br>" . $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<title>Logga in</title>
<meta charset="utf-8">
<style type="text/css">
body {
font-family: Arial;
}
h1 {
margin-top: 2em;
text-align: center;
}
form {
display: block;
/*border: 1px solid black;*/
width: calc(17vw + 2em);
margin: 3em auto;
}
input[type="text"], input[type="password"] {
display: block;
width: 17vw;
margin: 1em;
height: 2.2em;
padding: 0.4em;
border: 1px solid gray;
}
input[type="submit"] {
display: block;
width: 7em;
padding: 0.5em;
margin: 2em auto;
}
#message {
margin-top: 2em;
font-size: 13px;
color: red;
font-weight: bold;
text-align: center;
}
</style>
</head>
<body>
<h1>Logga in</h1>
<form method="post" action="">
<input type="text" name="username" placeholder="Användarnamn" required>
<input type="password" name="password" placeholder="Lösenord" required>
<input type="submit" name="submitted" value="Logga in">
</form>
<div id="message"><?= $message ?></div>
</body>
</html>