-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup_handler.php
More file actions
86 lines (72 loc) · 2.06 KB
/
Copy pathsignup_handler.php
File metadata and controls
86 lines (72 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
session_start();
require 'db.php';
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = $_POST['password'];
$confirm = $_POST['confirm_password'];
$errors = [];
if (!preg_match("/^[a-zA-Z0-9_]{3,20}$/", $username))
{
$errors[] = "Username must be 3-20 characters and contain only letters, numbers, or underscores.";
}
if (!preg_match("/^[\w\-\.]+@([\w-]+\.)+[\w-]{2,4}$/", $email))
{
$errors[] = "Invalid email format.";
}
if (!preg_match("/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[\W_]).{8,}$/", $password))
{
$errors[] = "Password must be at least 8 characters with upper, lower, digit, and special char.";
}
if ($password !== $confirm)
{
$errors[] = "Passwords do not match.";
}
if (empty($errors))
{
try
{
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
$stmt->execute([$username, $email]);
if ($stmt->fetch())
{
$errors[] = "Username or email already taken.";
}
}
catch (PDOException $e)
{
$errors[] = "Database error: " . $e->getMessage();
}
}
if (!empty($errors))
{
$_SESSION['signup_errors'] = $errors;
$_SESSION['old_input'] = ['username' => $username, 'email' => $email];
header("Location: signup.php");
exit;
}
$hash = password_hash($password, PASSWORD_DEFAULT);
try
{
$stmt = $pdo->prepare("INSERT INTO users (username, email, password_hash) VALUES (?, ?, ?)");
$stmt->execute([$username, $email, $hash]);
$_SESSION['username'] = $username;
$_SESSION['role'] = 'user';
if (!empty($_POST['remember']))
{
$token = bin2hex(random_bytes(32));
setcookie('remember_token', $token, time() + (86400 * 30), "/");
// Store token in DB
$stmt = $pdo->prepare("UPDATE users SET remember_token = ? WHERE username = ?");
$stmt->execute([$token, $username]);
}
header("Location: index.php");
exit;
}
catch (PDOException $e)
{
$_SESSION['signup_errors'] = ["Database insert error: " . $e->getMessage()];
$_SESSION['old_input'] = ['username' => $username, 'email' => $email];
header("Location: signup.php");
exit;
}