Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/submissions/projekt-quiz/
README.md
admin/
create_admin.php
create_quiz.php
css/
dashboard.php
debug_questions.php
includes/
index.php
leaderboard.php
login.php
logout.php
my_results.php
quiz.php
quiz_result.php
register.php
sqlcredentials
register.php
139 lines UTF-8 Unix (LF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
<?php
session_start();
require_once 'includes/db_connect.php';
$error = '';
$success = '';
// PROCESSERING
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name']);
$username = trim($_POST['username']);
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
// Validering
if (empty($name) || empty($username) || empty($password)) {
$error = "Alla fält måste fyllas i.";
}
elseif (strlen($username) < 3) {
$error = "Användarnamn måste vara minst 3 tecken.";
}
elseif (strlen($password) < 6) {
$error = "Lösenord måste vara minst 6 tecken.";
}
elseif ($password !== $confirm_password) {
$error = "Lösenorden matchar inte.";
}
else {
// Kolla om användarnamn redan finns
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = :username");
$stmt->execute([':username' => $username]);
if ($stmt->fetch()) {
$error = "Användarnamnet är redan taget.";
} else {
// Skapa användare
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("
INSERT INTO users (name, username, password, created_at)
VALUES (:name, :username, :password, NOW())
");
$stmt->execute([
':name' => $name,
':username' => $username,
':password' => $hashed_password
]);
// Logga in användaren direkt
$_SESSION['user_id'] = $pdo->lastInsertId();
$_SESSION['username'] = $username;
$_SESSION['name'] = $name;
$_SESSION['logged_in'] = true;
header("Location: dashboard.php");
exit();
}
}
}
$page_title = 'Registrera';
require_once 'includes/header.php';
?>
<div class="container">
<div class="register-box">
<h1>Skapa konto</h1>
<?php if ($error): ?>
<div class="error-message">
<?php echo htmlspecialchars($error); ?>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="success-message">
<?php echo htmlspecialchars($success); ?>
<p><a href="login.php">Gå till inloggning →</a></p>
</div>
<?php endif; ?>
<form method="POST" action="register.php">
<div class="form-group">
<label for="name">Namn:</label>
<input
type="text"
id="name"
name="name"
value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''; ?>"
required
>
</div>
<div class="form-group">
<label for="username">Användarnamn:</label>
<input
type="text"
id="username"
name="username"
value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>"
required
>
</div>
<div class="form-group">
<label for="password">Lösenord:</label>
<input
type="password"
id="password"
name="password"
required
>
</div>
<div class="form-group">
<label for="confirm_password">Bekräfta lösenord:</label>
<input
type="password"
id="confirm_password"
name="confirm_password"
required
>
</div>
<button type="submit" class="btn btn-primary">Registrera</button>
</form>
<p class="login-link">
Har du redan ett konto? <a href="login.php">Logga in här</a>
</p>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>