-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-session-ajax.php
More file actions
68 lines (56 loc) · 2.32 KB
/
validate-session-ajax.php
File metadata and controls
68 lines (56 loc) · 2.32 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
<?php
/**
* AJAX SESSION VALIDATOR (WITH ONE-DEVICE MODE CHECK)
* File: validate-session-ajax.php
* Returns JSON response indicating if session is valid
*
* NEW: Now checks if user has one_device_mode enabled before validating
*/
session_start();
header('Content-Type: application/json');
// Suppress errors
error_reporting(0);
@ini_set('display_errors', 0);
$response = ['status' => 'invalid'];
// Check if user is logged in
if (isset($_SESSION['staff_user']) && isset($_SESSION['user_id']) && isset($_SESSION['session_token'])) {
include 'db_connect.php';
$session_token = $_SESSION['session_token'];
$user_id = $_SESSION['user_id'];
// ✅ NEW: Check if user has one_device_mode enabled
$check_sql = "SELECT session_token, one_device_mode FROM staff_users WHERE id = " . intval($user_id) . " LIMIT 1";
$check_result = @mysqli_query($conn, $check_sql);
if ($check_result && mysqli_num_rows($check_result) > 0) {
$db_row = mysqli_fetch_assoc($check_result);
$db_session_token = $db_row['session_token'];
$one_device_mode = $db_row['one_device_mode'] ?? 'enabled'; // Default to enabled if column doesn't exist
// ✅ If one_device_mode is disabled, user can have multiple sessions
if ($one_device_mode === 'disabled') {
// Multi-device allowed - session is always valid
$response = ['status' => 'valid', 'mode' => 'multi-device'];
} else {
// ✅ One-device mode enabled - check session token
if ($session_token === $db_session_token) {
// Session is valid
$response = ['status' => 'valid', 'mode' => 'single-device'];
} else {
// Device conflict - session invalid
$response = ['status' => 'invalid', 'reason' => 'device_conflict'];
// Destroy session
session_unset();
session_destroy();
session_start();
$_SESSION['logout_reason'] = 'device_conflict';
}
}
} else {
// User not found in database
$response = ['status' => 'invalid', 'reason' => 'user_not_found'];
session_unset();
session_destroy();
}
@mysqli_close($conn);
}
echo json_encode($response);
exit();
?>