Login/register added

master
Filip Rojek 1 month ago
parent 0ad948e523
commit 4848d15d6d

@ -0,0 +1,19 @@
services:
mariadb:
container_name: tyna-db
image: mariadb:11.4 # LTS at 25. 12. 2025
restart: on-failure:2
environment:
MARIADB_ROOT_PASSWORD: root
ports:
- 3306:3306
phpmyadmin:
container_name: tyna-phpmyadmin
image: phpmyadmin
restart: on-failure:2
ports:
- 8080:80
environment:
- PMA_ARBITRARY=1

@ -0,0 +1,7 @@
<?php
// error reporting
ini_set('display_errors', '1');
require_once "environment.php";
require_once "database.php";

@ -6,5 +6,6 @@ ini_set('display_errors', '1');
require_once "environment.php";
require_once "database.php";
if(!$_SESSION['user']) {
header("Location: pages/login.php");
}

@ -0,0 +1,4 @@
<?php
session_start();
print_r($_SESSION['user']);
var_dump($_SESSION);

@ -0,0 +1,124 @@
<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Přihlášení - Knihomolův deník</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.login-container {
background-color: white;
padding: 2rem;
border: 1px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
text-align: center;
width: 100%;
max-width: 400px;
}
h1 {
margin-bottom: 1rem;
}
form {
display: flex;
flex-direction: column;
}
label {
text-align: left;
margin-bottom: 0.5rem;
font-weight: bold;
}
input {
margin-bottom: 1rem;
padding: 0.5rem;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 1rem;
}
button {
padding: 0.5rem;
background-color: #333;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1rem;
}
button:hover {
background-color: #555;
}
.error {
color: red;
margin-bottom: 1rem;
}
</style>
</head>
<body>
<div class="login-container">
<h1>Přihlášení</h1>
<form action="login.php" method="POST">
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Zadejte svůj email" required>
<label for="password">Heslo:</label>
<input type="password" id="password" name="password" placeholder="Zadejte své heslo" required>
<button type="submit">Přihlásit se</button>
</form>
<hr>
<a href="register.php">Registrace</a>
</div>
</body>
</html>
<?php
session_start();
require_once "../header.php";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Collect and sanitize input
$email = trim($_POST['email']);
$password = $_POST['password'];
// Basic validation
if (empty($email) || empty($password) ) {
die('All fields are required.');
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
die('Invalid email format.');
}
$db = Database::getInstance()->getConnection();
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
$stmt = $db->prepare("SELECT id, username, password FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
if ($result->num_rows === 1) {
$user = $result->fetch_assoc();
if (password_verify($password, $user['password'])) {
$_SESSION['user'] = [
'id' => $user['id'],
'username' => $user['username'],
'email' => $email,
];
header("Location: dashboard.php");
}
return "Incorrect username or password.";
} else {
return "Incorrect username or password.";
}
}

@ -0,0 +1,106 @@
<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registrace</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
form {
background-color: white;
padding: 2rem;
border: 1px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
input {
width: 100%;
padding: 0.5rem;
margin: 0.5rem 0;
border: 1px solid #ddd;
border-radius: 5px;
}
button {
background-color: #333;
color: white;
padding: 0.5rem 1rem;
border: none;
cursor: pointer;
font-size: 1rem;
}
button:hover {
background-color: #555;
}
</style>
</head>
<body>
<form action="register.php" method="POST">
<h2>Registrace</h2>
<label for="name">Jméno:</label>
<input type="text" id="name" name="username" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="password">Heslo:</label>
<input type="password" id="password" name="password" required>
<button type="submit">Registrovat</button>
</form>
</body>
</html>
<?php
session_start();
require_once "../header.php";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Collect and sanitize input
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = $_POST['password'];
// Basic validation
if (empty($username) || empty($email) || empty($password) ) {
die('All fields are required.');
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
die('Invalid email format.');
}
$db = Database::getInstance()->getConnection();
// Check if email already exists
$stmt = $db->prepare("SELECT id FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
if ($result->num_rows > 0) {
return "Email is already registered";
}
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
$stmt = $db->prepare("INSERT INTO users (username, email, password, created_at) VALUES (?, ?, ?, NOW())");
$stmt->bind_param("sss", $username, $email, $hashedPassword);
if ($stmt->execute()) {
return true;
} else {
return "Error: " . $stmt->error;
}
header("Location: login.php");
}
Loading…
Cancel
Save