-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.php
90 lines (87 loc) · 3.4 KB
/
main.php
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
<?php
// We need to use sessions, so you should always start sessions using the below code.
session_start();
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'christianvillads_techwasd';
$DATABASE_PASS = 'Aspit1234';
$DATABASE_NAME = 'christianvillads_techwasd';
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if (mysqli_connect_errno()) {
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
// The below function will check if the user is logged-in and also check the remember me cookie
function checkLoggedIn($con)
{
// You can add the remember me part below in all your files that require it (home, profile, etc).
if (isset($_COOKIE['rememberme']) && !empty($_COOKIE['rememberme']) && !isset($_SESSION['loggedin'])) {
// If the remember me cookie matches one in the database then we can update the session variables.
$stmt = $con->prepare('SELECT id, username FROM accounts WHERE rememberme = ?');
$stmt->bind_param('s', $_COOKIE['rememberme']);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
} else {
// If the user is not logged in redirect to the login page.
header('Location: login.php');
exit;
}
} else if (!isset($_SESSION['loggedin'])) {
// If the user is not logged in redirect to the login page.
header('Location: login.php');
exit;
}
}
/*
if (isset($_SESSION['loggedin'])) {
$stmt = $con->prepare('SELECT user_id, last_heartbeat FROM online_users');
$stmt->execute();
$result = $stmt->get_result();
$users = $result->fetch_all(MYSQLI_ASSOC);
if (isUserOnline($users)) {
foreach ($users as $row) {
$currentNewTime = new DateTime('now');
$result = $currentNewTime->format('Y-m-d H:i:s');
if ($row['user_id'] == $_SESSION['id']) {
$stmt = $con->prepare('UPDATE online_users SET last_heartbeat = ? WHERE user_id = ?');
$stmt->bind_param('ss', $result, $_SESSION['id']);
$stmt->execute();
$stmt->close();
}
}
foreach ($users as $row) {*/
/*
$myDateTime = DateTime::createFromFormat('Y-m-d H:i:s', $result);
$myDateTime2 = DateTime::createFromFormat('Y-m-d H:i:s', $dtNow);
$dtToCompare = $myDateTime;
$diff = $dtNow - $dtToCompare;
$newTime = $row['last_heartbeat'];
$dtNow = new DateTime();
$result = $dtNow->format('Y-m-d H:i:s');
$timeFirst = strtotime($result);
$timeSecond = strtotime($newTime);
$diff = ($timeFirst - $timeSecond);
if ($diff > 300) {
$stmt = $con->prepare('DELETE FROM online_users WHERE user_id = ?');
$stmt->bind_param('i', $row['user_id']);
$stmt->execute();
}
}
} else {
$currentNewTime = new DateTime('now');
$result = $currentNewTime->format('Y-m-d H:i:s');
$stmt = $con->prepare('INSERT INTO online_users (user_id, last_heartbeat) VALUES (?, ?)');
$stmt->bind_param('ss', $_SESSION['id'], $result);
$stmt->execute();
$stmt->close();
}
}
*/
function isUserOnline($users)
{
foreach ($users as $row) {
if ($row['user_id'] == $_SESSION['id']) {
return true;
}
}
return false;
}