-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticate.php
More file actions
114 lines (106 loc) · 6.2 KB
/
authenticate.php
File metadata and controls
114 lines (106 loc) · 6.2 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
<?php
// Start session first
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" : [];
// Fetch tasks (unchanged, but could be removed if not needed here)
$stmt = $pdo->query("SELECT * FROM tasks ORDER BY created_at DESC");
$tasks = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Handle user login authentication
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$email = trim($_POST["email"] ?? "");
$password = $_POST["password"] ?? "";
$error = "";
if ($email === "" || $password === "") {
$error = $lang['all_fields_required'] ?? "Email and password are required.";
} else {
$stmt = $pdo->prepare("SELECT id, username, password FROM users WHERE email = :email");
$stmt->execute(['email' => $email]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
// Redirect with language preserved
header("Location: index.php?lang=" . urlencode($selected_lang));
exit();
} else {
$error = $lang['invalid_credentials'] ?? "Invalid email or password.";
}
}
}
?>
<!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['log_in'] ?? 'Log In'); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet">
<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; }
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" style="max-width: 400px;">
<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['log_in'] ?? 'Log In'); ?></h1>
<?php if (!empty($error)): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'] . '?lang=' . urlencode($selected_lang)); ?>">
<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>
</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>
</div>
<button type="submit" class="btn btn-primary"><?php echo htmlspecialchars($lang['log_in'] ?? 'Log In'); ?></button>
<a href="registration.php?lang=<?php echo urlencode($selected_lang); ?>" class="btn btn-secondary"><?php echo htmlspecialchars($lang['register'] ?? 'Register'); ?></a>
</form>
</div>
</body>
</html>