-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistration_process.php
More file actions
103 lines (86 loc) · 3.94 KB
/
Copy pathregistration_process.php
File metadata and controls
103 lines (86 loc) · 3.94 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
session_start();
// Include secure database configuration
require_once './config/database.php';
$message = "";
if (isset($_POST['username'])) {
try {
// CSRF token validation
if (!isset($_POST['csrf_token']) || !validateCSRFToken($_POST['csrf_token'])) {
throw new Exception("Invalid CSRF token");
}
// Input validation and sanitization
$firstName = sanitizeInput($_POST['firstName']);
$lastName = sanitizeInput($_POST['lastName']);
$middleInitial = sanitizeInput($_POST['middleInitial']);
$suffix = sanitizeInput($_POST['suffix']);
$address = sanitizeInput($_POST['address']);
$nationality = sanitizeInput($_POST['nationality']);
$religion = sanitizeInput($_POST['religion']);
$civilStatus = sanitizeInput($_POST['civilStatus']);
$username = sanitizeInput($_POST['username']);
$email = sanitizeInput($_POST['email']);
$password = $_POST['password'];
$confirmPassword = $_POST['confirmPassword'];
$birthdate = sanitizeInput($_POST['birthdate']);
$age = (int)$_POST['age'];
$sex = sanitizeInput($_POST['sex']);
// Server-side validation
if (!validateRequired($firstName) || !validateRequired($lastName) ||
!validateRequired($username) || !validateRequired($email) ||
!validateRequired($password)) {
throw new Exception("All required fields must be filled");
}
if (!validateEmail($email)) {
throw new Exception("Invalid email format");
}
if (strlen($password) < 8) {
throw new Exception("Password must be at least 8 characters long");
}
if ($password !== $confirmPassword) {
throw new Exception("Passwords do not match");
}
// Get database instance
$db = Database::getInstance();
// Check if username or email already exists
$checkQuery = "SELECT COUNT(*) FROM users WHERE username = :username OR email = :email";
$stmt = $db->prepare($checkQuery);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':email', $email);
$stmt->execute();
if ($stmt->fetchColumn() > 0) {
throw new Exception("Username or email already exists");
}
// Hash password securely
$hashedPassword = hashPassword($password);
// Insert new user with prepared statement
$insertQuery = "INSERT INTO users (first_name, last_name, middle_initial, suffix, address, nationality, religion, civil_status, username, email, password, birthdate, age, sex)
VALUES (:firstName, :lastName, :middleInitial, :suffix, :address, :nationality, :religion, :civilStatus, :username, :email, :password, :birthdate, :age, :sex)";
$stmt = $db->prepare($insertQuery);
$stmt->bindParam(':firstName', $firstName);
$stmt->bindParam(':lastName', $lastName);
$stmt->bindParam(':middleInitial', $middleInitial);
$stmt->bindParam(':suffix', $suffix);
$stmt->bindParam(':address', $address);
$stmt->bindParam(':nationality', $nationality);
$stmt->bindParam(':religion', $religion);
$stmt->bindParam(':civilStatus', $civilStatus);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':password', $hashedPassword);
$stmt->bindParam(':birthdate', $birthdate);
$stmt->bindParam(':age', $age);
$stmt->bindParam(':sex', $sex);
if ($stmt->execute()) {
$message = "User successfully registered";
} else {
throw new Exception("Registration failed. Please try again.");
}
} catch (Exception $e) {
$message = $e->getMessage();
error_log("Registration error: " . $e->getMessage());
}
$_SESSION['message'] = $message;
header('Location: index.php');
exit();
}