except remember user, changed all cookies to sessions.
This commit is contained in:
@@ -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) {
|
||||
|
||||
+10
-18
@@ -21,23 +21,15 @@ if (!isset($_SESSION['initialized'])) {
|
||||
}
|
||||
|
||||
// Update the database and cookies to keep the user logged in.
|
||||
if (isset($_COOKIE['user_id'])) {
|
||||
if (isset($_SESSION['user_id'])) {
|
||||
$conn = get_connection();
|
||||
$stmt = $conn->prepare('UPDATE user SET is_active = true WHERE user_id = ?');
|
||||
$stmt->bind_param('i', $_COOKIE['user_id']);
|
||||
$stmt->bind_param('i', $_SESSION['user_id']);
|
||||
$stmt->execute();
|
||||
|
||||
$stmt = $conn->prepare('UPDATE user SET last_active = CURRENT_TIMESTAMP WHERE user_id = ?');
|
||||
$stmt->bind_param('i', $_COOKIE['user_id']);
|
||||
$stmt->bind_param('i', $_SESSION['user_id']);
|
||||
$stmt->execute();
|
||||
|
||||
$stmt = $conn->prepare('SELECT user_id, username, email, role_id, profile_picture FROM user WHERE user_id = ?');
|
||||
$stmt->bind_param('i', $_COOKIE['user_id']);
|
||||
$stmt->execute();
|
||||
$user = $stmt->get_result()->fetch_assoc();
|
||||
|
||||
$time = time() + 60 * 15;
|
||||
cookies($user, $time);
|
||||
} else if (isset($_COOKIE['remember_user'])) {
|
||||
$conn = get_connection();
|
||||
$stmt = $conn->prepare('SELECT * FROM remember_user WHERE token = ?');
|
||||
@@ -49,21 +41,21 @@ if (isset($_COOKIE['user_id'])) {
|
||||
// TODO: Log the attempt to use a cookie from a different browser/device than the cookie was created
|
||||
echo "Naughty, you are trying to use someone else's cookie...";
|
||||
} else {
|
||||
$stmt = $conn->prepare('SELECT user_id, username, email, role_id, profile_picture FROM user WHERE user_id = ?');
|
||||
$stmt = $conn->prepare('SELECT user_id, username, role_id FROM user WHERE user_id = ?');
|
||||
$stmt->bind_param('s', $remember['user_id']);
|
||||
$stmt->execute();
|
||||
$user = $stmt->get_result()->fetch_assoc();
|
||||
|
||||
$conn = get_connection();
|
||||
$_SESSION['user_id'] = $user['user_id'];
|
||||
$_SESSION['username'] = $user['username'];
|
||||
$_SESSION['role_id'] = $user['role_id'];
|
||||
|
||||
$stmt = $conn->prepare('UPDATE user SET is_active = true WHERE user_id = ?');
|
||||
$stmt->bind_param('i', $_COOKIE['user_id']);
|
||||
$stmt->bind_param('i', $_SESSION['user_id']);
|
||||
$stmt->execute();
|
||||
|
||||
$stmt = $conn->prepare('UPDATE user SET last_active = CURRENT_TIMESTAMP WHERE user_id = ?');
|
||||
$stmt->bind_param('i', $_COOKIE['user_id']);
|
||||
$stmt->bind_param('i', $_SESSION['user_id']);
|
||||
$stmt->execute();
|
||||
|
||||
$time = time() + 60 * 15;
|
||||
cookies($user, $time);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user