except remember user, changed all cookies to sessions.

This commit is contained in:
2025-04-13 20:07:07 -06:00
parent 006cffef38
commit 3ef61e1c9e
24 changed files with 42 additions and 158 deletions
+6 -21
View File
@@ -22,34 +22,19 @@ function username_exists($username)
return true;
}
function cookies($user, $time)
{
setcookie('user_id', $user['user_id'], $time);
setcookie('username', $user['username'], $time);
setcookie('email', $user['email'], $time);
setcookie('role_id', $user['role_id'], $time);
setcookie('profile_picture', $user['profile_picture'], $time);
}
function logout()
{
foreach (array_keys($_COOKIE) as $key) {
setcookie($key, '', time() - 3600, '/');
unset($_COOKIE[$key]);
}
foreach (array_keys($_SESSION) as $key) {
foreach (array_keys($_SESSION) as $key)
unset($_SESSION[$key]);
}
header('Location: articles.php');
header('Location: .');
}
function login($username, $password, $stay_logged_in)
{
$password_hash = hash('sha256', $password);
$user = exec_stmt('SELECT user_id, username, email, role_id, profile_picture FROM user WHERE username = ? OR email = ? AND password_hash = ?', 'sss', $username, $username, $password_hash)->fetch_assoc();
$user = exec_stmt('SELECT user_id, username, role_id FROM user WHERE username = ? OR email = ? AND password_hash = ?', 'sss', $username, $username, $password_hash)->fetch_assoc();
if ($user == null) {
$error_msg = '
@@ -61,8 +46,8 @@ function login($username, $password, $stay_logged_in)
return $error_msg;
}
$time = time() + 60 * 60 * 1;
cookies($user, $time);
$_SESSION['user_id'] = $user['user_id'];
$_SESSION['username'] = $user['role_id'];
if ($stay_logged_in) {
$token = bin2hex(random_bytes(64));
@@ -247,7 +232,7 @@ function delete_account($user_id, $password = null, $verify_password = null)
exec_stmt('DELETE FROM user WHERE user_id = ?', 'i', $user_id);
logout();
} else if ($_COOKIE['role_id'] <= 2) {
} else if ($_SESSION['role_id'] <= 2) {
$user = exec_stmt('SELECT user_id, profile_picture FROM user WHERE user_id = ?', 'i', $user_id)->fetch_assoc();
if ($user['user_id'] == 2025) {