commit 539627366e3a84931178ced1cb514dc9eb308e0a Author: Pauljd Date: Sat Sep 5 11:52:25 2026 +0100 Inital setup diff --git a/db.php b/db.php new file mode 100644 index 0000000..5e4fc7e --- /dev/null +++ b/db.php @@ -0,0 +1,19 @@ +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; +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..88647f3 --- /dev/null +++ b/index.php @@ -0,0 +1,28 @@ + + + + + + Welcome + + + +

Welcome

+ +

Welcome guest

+ +

Happy ! We're glad you're here.

+ +

Log out

+ +

Sign Up

+

Log In

+ + + diff --git a/login.php b/login.php new file mode 100644 index 0000000..9432538 --- /dev/null +++ b/login.php @@ -0,0 +1,67 @@ +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; + } + } +} +?> + + + + + Log In + + + +
+ +
+
+ + + +
+ +
+
+ + + +
+ +
+ +
+ + diff --git a/logout.php b/logout.php new file mode 100644 index 0000000..0150ba0 --- /dev/null +++ b/logout.php @@ -0,0 +1,17 @@ + time() - 3600, + 'path' => '/', + 'httponly' => true, + 'samesite' => 'Strict', +]); + +header('Location: /'); +exit; diff --git a/register.php b/register.php new file mode 100644 index 0000000..6eb9fd4 --- /dev/null +++ b/register.php @@ -0,0 +1,105 @@ + 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; + } +} +?> + + + + + Register + + +
+
+ + + +
+ +
+
+ + + +
+ +
+
+ + + +
+ +
+ +
+ + diff --git a/session.php b/session.php new file mode 100644 index 0000000..f311d54 --- /dev/null +++ b/session.php @@ -0,0 +1,14 @@ + 0, + 'path' => '/', + 'httponly' => true, + 'samesite' => 'Strict', + ]); + session_start(); +}