Skip to content

Commit 1e346d7

Browse files
Cookies done
1 parent b59a613 commit 1e346d7

9 files changed

Lines changed: 120 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The project is ready for use but is still evolving. Work is ongoing to fix bugs
1515
1 - cookies for users, db collects data for analytics
1616
2 - tables for cookies added
1717
3 - JS folder
18+
4 - popout for cookies
1819
.....
1920

2021
### Changed

HOW_TO_RUN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ CorelyPHP is a ready-to-deploy blogging platform designed for efficient web deve
88

99
Before you begin, ensure you have the following installed:
1010

11-
- PHP (>= 8.0)
12-
- MySQL (or another compatible database)
13-
- XAMPP
11+
- PHP (>= 8.0) <!--(in XAMPP/MAMP)-->
12+
- MySQL (or another compatible database) <!--(in XAMPP/MAMP)-->
13+
- XAMPP / MAMP
1414
- Git
1515

1616
## Installation

public/CSS/popout.css

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.cookie-popup {
2+
position: fixed;
3+
bottom: 20px;
4+
left: 50%;
5+
transform: translateX(-50%);
6+
background-color: #333;
7+
color: #fff;
8+
padding: 15px 20px;
9+
border-radius: 8px;
10+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
11+
display: none;
12+
}
13+
14+
.cookie-popup a {
15+
color: #fff;
16+
text-decoration: underline;
17+
}
18+
19+
.cookie-popup button {
20+
background-color: #fff;
21+
color: #333;
22+
border: none;
23+
padding: 8px 12px;
24+
margin-left: 10px;
25+
cursor: pointer;
26+
border-radius: 5px;
27+
font-weight: bold;
28+
}
29+
30+
.cookie-popup button:hover {
31+
background-color: #ddd;
32+
}
33+
34+
.cookie-popup button#reject-cookies {
35+
background-color: #ff4d4d; /* Czerwony dla opcji odrzucenia */
36+
color: white;
37+
}
38+
39+
.cookie-popup button#reject-cookies:hover {
40+
background-color: #cc0000
41+
}

public/JS/popout.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
document.addEventListener("DOMContentLoaded", function () {
3+
const popup = document.getElementById("cookie-popup");
4+
const acceptButton = document.getElementById("accept-cookies");
5+
const rejectButton = document.getElementById("reject-cookies");
6+
7+
const cookiesAccepted = document.cookie.includes("cookiesAccepted=true");
8+
const cookiesRejected = document.cookie.includes("cookiesAccepted=false");
9+
10+
if (!cookiesAccepted && !cookiesRejected) {
11+
popup.style.display = "block";
12+
}
13+
14+
function setCookie(name, value, days) {
15+
let expires = "";
16+
if (days) {
17+
const date = new Date();
18+
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
19+
expires = "; expires=" + date.toUTCString();
20+
}
21+
document.cookie = name + "=" + value + expires + "; path=/server/CorelyPHP-1.1.0/public/";
22+
}
23+
24+
// Obsługa zgody na ciasteczka
25+
acceptButton.addEventListener("click", function () {
26+
setCookie("cookiesAccepted", "true", 365);
27+
popup.style.display = "none";
28+
location.reload();
29+
});
30+
31+
// Obsługa odrzucenia ciasteczek
32+
rejectButton.addEventListener("click", function () {
33+
setCookie("cookiesAccepted", "false", 365);
34+
popup.style.display = "none";
35+
location.reload();
36+
});
37+
});
38+
39+

public/error_404.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,9 @@
5959
?>
6060
</footer>
6161

62+
<?php
63+
cookie_popout()
64+
?>
65+
6266
</body>
6367
</html>

public/handlers/cookies.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
require 'visits.alg.php';
44

55
function cookie($page): void {
6+
if (isset($_COOKIE['cookiesAccepted']) && $_COOKIE['cookiesAccepted'] === 'true') {
7+
setcookie("cookiesAccepted", "true", time() + (3600 * 24 * 365), "/server/CorelyPHP-1.1.0/public/");
68

7-
if (!isset($_COOKIE['visitor_id'])) {
9+
if (!isset($_COOKIE['visitor_id'])) {
10+
$cookie_id = bin2hex(random_bytes(16));
11+
setcookie('visitor_id', $cookie_id, time() + (3600 * 24 * 365), "/server/CorelyPHP-1.1.0/public/");
12+
} else {
13+
$cookie_id = $_COOKIE['visitor_id'];
14+
}
815

9-
$cookie_id = bin2hex(random_bytes(16));
10-
setcookie('visitor_id', $cookie_id, time() + (3600 * 24 * 365), "/CorelyPHP-1.1.0/public/"); // path may need to be changed
16+
updateVisitCount($cookie_id, $page);
1117
} else {
12-
$cookie_id = $_COOKIE['visitor_id'];
18+
setcookie('visitor_id', "", time() - 3600, "/server/CorelyPHP-1.1.0/public/");
1319
}
14-
15-
updateVisitCount($cookie_id, $page);
16-
1720
}
18-
19-
//$page = basename($_SERVER['PHP_SELF']);
21+

public/handlers/functions.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,14 @@ function foot(): void{
7575
</div>";
7676

7777
}
78+
79+
function cookie_popout(): void {
80+
81+
echo '
82+
<div id="cookie-popup" class="cookie-popup">
83+
<p>This website uses cookies to improve your experience. You can accept or reject them.</p>
84+
<button id="accept-cookies">Accept</button>
85+
<button id="reject-cookies">Reject</button>
86+
<a href="" id="cookie-settings">Settings</a>
87+
';
88+
}

public/main.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<title>Blog Home Page</title>
2121

2222
<link rel="stylesheet" href="./CSS/main.css" />
23+
<link rel="stylesheet" href="./CSS/popout.css" />
2324

2425
<script src="./JS/popout.js"></script>
2526

@@ -52,5 +53,10 @@
5253
?>
5354
</footer>
5455

56+
<?php
57+
cookie_popout()
58+
?>
59+
60+
</div>
5561
</body>
5662
</html>

public/single.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,9 @@
6464
?>
6565
</footer>
6666

67+
<?php
68+
cookie_popout()
69+
?>
70+
6771
</body>
6872
</html>

0 commit comments

Comments
 (0)