-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistration.php
More file actions
156 lines (145 loc) · 8.62 KB
/
registration.php
File metadata and controls
156 lines (145 loc) · 8.62 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
// Start session to persist language
session_start();
require_once "config.php";
// Language selection logic
$available_langs = [
'en' => 'English',
'ru' => 'Русский',
'es' => 'Español',
'de' => 'Deutsch',
'pt' => 'Português',
'tr' => 'Türkçe',
'zh-CN' => '中文',
'uz' => 'Oʻzbekcha',
'fa' => 'فارسی',
'ar' => 'العربية'
];
// Determine selected language: GET > Session > Default
$selected_lang = $_GET['lang'] ?? $_SESSION['lang'] ?? 'en';
if (!array_key_exists($selected_lang, $available_langs)) {
$selected_lang = 'en';
}
// Save selected language to session
$_SESSION['lang'] = $selected_lang;
// Load translations
$lang = file_exists(__DIR__ . "/lang/{$selected_lang}.php") ? require __DIR__ . "/lang/{$selected_lang}.php" : [];
// Initialize variables
$username = $email = $password = "";
$username_err = $email_err = $password_err = "";
$successMsg = "";
// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate username
$username = trim($_POST["username"] ?? "");
if (empty($username)) {
$username_err = $lang['all_fields_required'] ?? "Username is required.";
} elseif (!preg_match("/^[a-zA-Z0-9_]+$/", $username)) {
$username_err = $lang['only_letters_numbers_underscores'] ?? "Only letters, numbers, and underscores are allowed.";
}
// Validate email
$email = trim($_POST["email"] ?? "");
if (empty($email)) {
$email_err = $lang['all_fields_required'] ?? "Email is required.";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_err = $lang['invalid_email'] ?? "Invalid email format.";
}
// Validate password
$password = trim($_POST["password"] ?? "");
if (empty($password)) {
$password_err = $lang['all_fields_required'] ?? "Password is required.";
} elseif (strlen($password) < 8) {
$password_err = $lang['password_length'] ?? "Password must be at least 8 characters.";
}
// Check for duplicate username/email
if (empty($username_err) && empty($email_err) && empty($password_err)) {
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = :username OR email = :email");
$stmt->execute(['username' => $username, 'email' => $email]);
if ($stmt->fetch()) {
$username_err = $lang['username_taken'] ?? "Username or email already exists.";
}
}
// Insert user if no errors
if (empty($username_err) && empty($email_err) && empty($password_err)) {
$hashed_password = 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' => $hashed_password]);
if ($stmt->rowCount()) {
// Log in the new user
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = :username");
$stmt->execute(['username' => $username]);
$user = $stmt->fetch();
$_SESSION['user_id'] = $user['id'];
// Redirect with language preserved
header("Location: index.php?lang=" . urlencode($selected_lang));
exit;
} else {
$successMsg = $lang['registration_failed'] ?? "Registration failed. Please try again.";
}
}
}
?>
<!DOCTYPE html>
<html lang="<?php echo htmlspecialchars($selected_lang); ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($lang["title_registration"] ?? "Registration"); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"></script>
<style>
* { text-decoration: none; box-sizing: border-box; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 0; padding: 0; list-style: none; outline: none; border: none; }
.error { color: red; font-size: 0.9em; }
.success { color: green; font-weight: bold; }
h1 { text-align: center; margin-bottom: 30px; letter-spacing: 2px; }
label { color: #1A202C; letter-spacing: 2px; font-weight: 500; font-size: 14px; margin-bottom: 5px; }
body { background-color: #E0E7FF; }
.container { max-width: 500px; background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
button, a { height: 45px; width: 100%; margin-top: 10px; margin-bottom: 10px; padding: 10px; text-decoration: none; color: inherit; cursor: pointer; }
.btn-primary { background-color: #4F46E5; border-color: #4F46E5; font-size: 16px; font-weight: 500; transition: background-color 0.3s, border-color 0.3s; box-shadow: 0 4px 6px rgba(79,70,229,0.1); border-radius: 6px; text-transform: uppercase; letter-spacing: 1px; cursor: pointer; }
button, a.btn-primary:hover { background-color: #4338CA; border-color: #4338CA; box-shadow: 0 6px 8px rgba(67,56,202,0.15); color: #fff; }
button, a.btn-primary:active { background-color: #3730A3; border-color: #3730A3; box-shadow: 0 2px 4px rgba(55,48,163,0.2); color: #fff; }
button, a.btn-primary:focus { outline: none; box-shadow: 0 0 0 3px rgba(79,70,229,0.4); color: #fff; }
input.form-control { border: 1px solid #CED4DA; padding: 10px; font-size: 14px; border-radius: 6px; transition: border-color 0.3s, box-shadow 0.3s; width: 100%; letter-spacing: 1px; font-weight: 500; color: #495057; background-color: #F8F9FA; }
input.form-control:focus { border-color: #4F46E5; box-shadow: 0 0 0 3px rgba(79,70,229,0.2); outline: none; }
input.form-control::placeholder { color: #6C757D; opacity: 1; }
input.form-control:disabled, input.form-control[readonly] { background-color: #E9ECEF; opacity: 1; cursor: not-allowed; }
.form-label { margin-bottom: 8px; font-weight: 600; display: block; letter-spacing: 1px; font-size: 13px; color: #495057; text-transform: uppercase; }
.mb-3 { margin-bottom: 15px; }
</style>
</head>
<body>
<div class="container mt-5">
<form method="get" class="mb-3" style="max-width:200px; float: right;">
<select name="lang" class="form-select" onchange="this.form.submit()">
<?php foreach ($available_langs as $key => $label): ?>
<option value="<?php echo htmlspecialchars($key); ?>" <?php if ($selected_lang === $key) echo 'selected'; ?>><?php echo htmlspecialchars($label); ?></option>
<?php endforeach; ?>
</select>
</form>
<h1><?php echo htmlspecialchars($lang["title_registration"] ?? "Registration"); ?></h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"] . "?lang=" . urlencode($selected_lang)); ?>" method="POST" class="mb-4">
<div class="mb-3">
<label for="username" class="form-label"><?php echo htmlspecialchars($lang["username"] ?? "Username"); ?></label>
<input type="text" class="form-control" id="username" name="username" required value="<?php echo htmlspecialchars($username); ?>">
<span class="error"><?php echo htmlspecialchars($username_err); ?></span>
</div>
<div class="mb-3">
<label for="email" class="form-label"><?php echo htmlspecialchars($lang["email"] ?? "Email"); ?></label>
<input type="email" class="form-control" id="email" name="email" required value="<?php echo htmlspecialchars($email); ?>">
<span class="error"><?php echo htmlspecialchars($email_err); ?></span>
</div>
<div class="mb-3">
<label for="password" class="form-label"><?php echo htmlspecialchars($lang["password"] ?? "Password"); ?></label>
<input type="password" class="form-control" id="password" name="password" required>
<span class="error"><?php echo htmlspecialchars($password_err); ?></span>
</div>
<button type="submit" class="btn btn-primary"><?php echo htmlspecialchars($lang["register"] ?? "Register"); ?></button>
<a href="authenticate.php?lang=<?php echo urlencode($selected_lang); ?>" class="btn btn-primary"><?php echo htmlspecialchars($lang["log_in"] ?? "Log In"); ?></a>
</form>
<?php if ($successMsg): ?>
<p class="success"><?php echo htmlspecialchars($successMsg); ?></p>
<?php endif; ?>
</div>
</body>
</html>