Inital setup
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
function getDbConnection(): PDO
|
||||
{
|
||||
static $pdo = null;
|
||||
|
||||
if ($pdo === null) {
|
||||
$dbPath = __DIR__ . '/../app.sqlite';
|
||||
$pdo = new PDO('sqlite:' . $dbPath);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$pdo->exec('CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
email TEXT NOT NULL UNIQUE,
|
||||
password TEXT NOT NULL
|
||||
)');
|
||||
}
|
||||
|
||||
return $pdo;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
require __DIR__ . '/session.php';
|
||||
start_app_session();
|
||||
|
||||
$dayOfWeek = date('l');
|
||||
$isLoggedIn = isset($_SESSION['username']);
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Welcome</title>
|
||||
</head>
|
||||
<body>
|
||||
<?php if ($isLoggedIn): ?>
|
||||
<h1>Welcome <?php echo htmlspecialchars($_SESSION['username'], ENT_QUOTES, 'UTF-8'); ?></h1>
|
||||
<?php else: ?>
|
||||
<h1>Welcome guest</h1>
|
||||
<?php endif; ?>
|
||||
<p>Happy <?php echo $dayOfWeek; ?>! We're glad you're here.</p>
|
||||
<?php if ($isLoggedIn): ?>
|
||||
<p><a href="logout.php">Log out</a></p>
|
||||
<?php else: ?>
|
||||
<p><a href="register.php">Sign Up</a></p>
|
||||
<p><a href="login.php">Log In</a></p>
|
||||
<?php endif; ?>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
require __DIR__ . '/session.php';
|
||||
require __DIR__ . '/db.php';
|
||||
|
||||
$errors = [];
|
||||
$username = '';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$username = trim($_POST['username'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
if ($username === '') {
|
||||
$errors['username'] = 'Username is required.';
|
||||
}
|
||||
|
||||
if ($password === '') {
|
||||
$errors['password'] = 'Password is required.';
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$pdo = getDbConnection();
|
||||
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
|
||||
$stmt->execute(['username' => $username]);
|
||||
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$user || !password_verify($password, $user['password'])) {
|
||||
$errors['login'] = 'Invalid username or password.';
|
||||
} else {
|
||||
start_app_session();
|
||||
session_regenerate_id(true);
|
||||
$_SESSION['username'] = $user['username'];
|
||||
|
||||
header('Location: /');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Log In</title>
|
||||
</head>
|
||||
<body>
|
||||
<?php if (isset($errors['login'])): ?>
|
||||
<div><?php echo htmlspecialchars($errors['login'], ENT_QUOTES, 'UTF-8'); ?></div>
|
||||
<?php endif; ?>
|
||||
<form action="login.php" method="post" novalidate>
|
||||
<div>
|
||||
<label for="username">Username</label>
|
||||
<input type="text" id="username" name="username" value="<?php echo htmlspecialchars($username, ENT_QUOTES, 'UTF-8'); ?>">
|
||||
<?php if (isset($errors['username'])): ?>
|
||||
<div><?php echo htmlspecialchars($errors['username'], ENT_QUOTES, 'UTF-8'); ?></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div>
|
||||
<label for="password">Password</label>
|
||||
<input type="password" id="password" name="password">
|
||||
<?php if (isset($errors['password'])): ?>
|
||||
<div><?php echo htmlspecialchars($errors['password'], ENT_QUOTES, 'UTF-8'); ?></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<button type="submit">Log In</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
require __DIR__ . '/session.php';
|
||||
|
||||
start_app_session();
|
||||
|
||||
$_SESSION = [];
|
||||
session_destroy();
|
||||
|
||||
setcookie(session_name(), '', [
|
||||
'expires' => time() - 3600,
|
||||
'path' => '/',
|
||||
'httponly' => true,
|
||||
'samesite' => 'Strict',
|
||||
]);
|
||||
|
||||
header('Location: /');
|
||||
exit;
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
require __DIR__ . '/session.php';
|
||||
require __DIR__ . '/db.php';
|
||||
|
||||
$errors = [];
|
||||
$username = '';
|
||||
$email = '';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$username = trim($_POST['username'] ?? '');
|
||||
$email = trim($_POST['email'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
|
||||
|
||||
$pdo = getDbConnection();
|
||||
|
||||
if ($username === '') {
|
||||
$errors['username'] = 'Username is required.';
|
||||
} elseif (strlen($username) < 3 || strlen($username) > 12) {
|
||||
$errors['username'] = 'Username must be between 3 and 12 characters long.';
|
||||
} elseif (!preg_match('/^[a-z0-9]+$/', $username)) {
|
||||
$errors['username'] = 'Username can only contain lowercase letters (a-z) and numbers (0-9).';
|
||||
} else {
|
||||
$stmt = $pdo->prepare('SELECT 1 FROM users WHERE username = :username');
|
||||
$stmt->execute(['username' => $username]);
|
||||
if ($stmt->fetch()) {
|
||||
$errors['username'] = 'That username is not available.';
|
||||
}
|
||||
}
|
||||
|
||||
if ($email === '') {
|
||||
$errors['email'] = 'Email is required.';
|
||||
} elseif (strlen($email) > 100) {
|
||||
$errors['email'] = 'Email cannot exceed 100 characters.';
|
||||
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$errors['email'] = 'Please enter a valid email address.';
|
||||
} else {
|
||||
$stmt = $pdo->prepare('SELECT 1 FROM users WHERE email = :email');
|
||||
$stmt->execute(['email' => $email]);
|
||||
if ($stmt->fetch()) {
|
||||
$errors['email'] = 'That email address is already registered.';
|
||||
}
|
||||
}
|
||||
|
||||
if ($password === '') {
|
||||
$errors['password'] = 'Password is required.';
|
||||
} elseif (strlen($password) > 100) {
|
||||
$errors['password'] = 'Password cannot exceed 100 characters.';
|
||||
} elseif (strlen($password) < 6 || strlen($password) > 12) {
|
||||
$errors['password'] = 'Password must be between 6 and 12 characters long.';
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
||||
|
||||
$stmt = $pdo->prepare('INSERT INTO users (username, email, password) VALUES (:username, :email, :password)');
|
||||
$stmt->execute([
|
||||
'username' => $username,
|
||||
'email' => $email,
|
||||
'password' => $hashedPassword,
|
||||
]);
|
||||
|
||||
start_app_session();
|
||||
session_regenerate_id(true);
|
||||
$_SESSION['username'] = $username;
|
||||
|
||||
header('Location: /');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Register</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="register.php" method="post" novalidate>
|
||||
<div>
|
||||
<label for="username">Username</label>
|
||||
<input type="text" id="username" name="username" value="<?php echo htmlspecialchars($username, ENT_QUOTES, 'UTF-8'); ?>">
|
||||
<?php if (isset($errors['username'])): ?>
|
||||
<div><?php echo htmlspecialchars($errors['username'], ENT_QUOTES, 'UTF-8'); ?></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div>
|
||||
<label for="email">Email</label>
|
||||
<input type="text" id="email" name="email" value="<?php echo htmlspecialchars($email, ENT_QUOTES, 'UTF-8'); ?>">
|
||||
<?php if (isset($errors['email'])): ?>
|
||||
<div><?php echo htmlspecialchars($errors['email'], ENT_QUOTES, 'UTF-8'); ?></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div>
|
||||
<label for="password">Password</label>
|
||||
<input type="password" id="password" name="password">
|
||||
<?php if (isset($errors['password'])): ?>
|
||||
<div><?php echo htmlspecialchars($errors['password'], ENT_QUOTES, 'UTF-8'); ?></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<button type="submit">Sign Up</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
function start_app_session(): void
|
||||
{
|
||||
if (session_status() === PHP_SESSION_ACTIVE) {
|
||||
return;
|
||||
}
|
||||
session_set_cookie_params([
|
||||
'lifetime' => 0,
|
||||
'path' => '/',
|
||||
'httponly' => true,
|
||||
'samesite' => 'Strict',
|
||||
]);
|
||||
session_start();
|
||||
}
|
||||
Reference in New Issue
Block a user