From b59a613511c182add1624cf58452882298fe0500 Mon Sep 17 00:00:00 2001 From: Dominik <163560221+Dominik-developer@users.noreply.github.com> Date: Tue, 11 Mar 2025 20:42:24 +0100 Subject: [PATCH 1/3] Cookies for users in /public --- CHANGELOG.md | 8 +++ admin/panel.connect.php | 2 +- database_SQL/blog_DB_data.sql | 17 +---- database_SQL/blog_DB_structure.sql | 97 +++++++++++---------------- public/{ => CSS}/main.css | 0 public/{ => CSS}/single.css | 0 public/JS/popout.js | 0 public/error.html | 9 ++- public/error_404.php | 16 +++-- public/{ => handlers}/connect.php | 2 +- public/handlers/cookies.php | 19 ++++++ public/{ => handlers}/functions.php | 24 +++---- public/handlers/index.php | 4 ++ public/{ => handlers}/service.alg.php | 4 +- public/{ => handlers}/single.alg.php | 4 +- public/handlers/visits.alg.php | 47 +++++++++++++ public/index.php | 2 +- public/main.php | 12 +++- public/service.html | 7 +- public/single.php | 18 +++-- 20 files changed, 184 insertions(+), 108 deletions(-) rename public/{ => CSS}/main.css (100%) rename public/{ => CSS}/single.css (100%) create mode 100644 public/JS/popout.js rename public/{ => handlers}/connect.php (76%) create mode 100644 public/handlers/cookies.php rename public/{ => handlers}/functions.php (82%) create mode 100644 public/handlers/index.php rename public/{ => handlers}/service.alg.php (93%) rename public/{ => handlers}/single.alg.php (94%) create mode 100644 public/handlers/visits.alg.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 21f10d9..7b08597 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,18 @@ The project is ready for use but is still evolving. Work is ongoing to fix bugs ### Added + +`/public` +1 - cookies for users, db collects data for analytics +2 - tables for cookies added +3 - JS folder ..... ### Changed + +`/public` +1 - structure of /public fixes ..... ### Fixed diff --git a/admin/panel.connect.php b/admin/panel.connect.php index 8171e0b..c2727bb 100644 --- a/admin/panel.connect.php +++ b/admin/panel.connect.php @@ -2,7 +2,7 @@ $host = "localhost"; $db_user = "root"; - $db_password =""; + $db_password ="root"; $db_name = "blog"; diff --git a/database_SQL/blog_DB_data.sql b/database_SQL/blog_DB_data.sql index 55147d4..6d51d41 100644 --- a/database_SQL/blog_DB_data.sql +++ b/database_SQL/blog_DB_data.sql @@ -1,22 +1,11 @@ -SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; START TRANSACTION; -SET time_zone = "+00:00"; - -USE `blog`; - --- --- Inserting data into table `admin` --- INSERT INTO `admin` (`id`, `login`, `password`) VALUES -(1, 'admin', '$2y$10$L9fQlnPTTuYkNhLnXh68..F8R.bJdLaAJBJjXU8RjhiHUHFVVJyCe'); +(1, 'admin', '$2y$10$w9GkWVGXxSTjw4A9QyjasuqyeJyUPp2JlWYnFGFEen7e2..YUXxNC'); --- --- Inserting data into table `service` --- +-- -------------------------------------------------------- INSERT INTO `service` (`id`, `service_status`) VALUES -(1, 1); - +(1, 0); COMMIT; diff --git a/database_SQL/blog_DB_structure.sql b/database_SQL/blog_DB_structure.sql index 20d0c42..77c857b 100644 --- a/database_SQL/blog_DB_structure.sql +++ b/database_SQL/blog_DB_structure.sql @@ -1,79 +1,64 @@ +-- Databse 'blog': structure SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; START TRANSACTION; SET time_zone = "+00:00"; -CREATE DATABASE IF NOT EXISTS `blog`; -USE `blog`; - -/*!40101 SET NAMES utf8mb4 */; - --- --- Table structure for table `admin` --- +-- -------------------------------------------------------- CREATE TABLE `admin` ( - `id` int(11) NOT NULL, - `login` varchar(25) NOT NULL, - `password` varchar(25) NOT NULL + `id` int NOT NULL AUTO_INCREMENT, + `login` varchar(25) COLLATE utf8mb4_general_ci NOT NULL, + `password` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; --- --- Table structure for table `articles` --- +-- -------------------------------------------------------- CREATE TABLE `articles` ( - `ID` int(11) NOT NULL, - `title` varchar(255) NOT NULL, - `text` text NOT NULL, - `photo_path` varchar(255) NOT NULL, - `date_of_publish` timestamp NOT NULL DEFAULT current_timestamp() + `ID` int NOT NULL AUTO_INCREMENT, + `title` varchar(255) COLLATE utf8mb4_general_ci NOT NULL, + `text` text COLLATE utf8mb4_general_ci NOT NULL, + `photo_path` varchar(255) COLLATE utf8mb4_general_ci NOT NULL, + `date_of_publish` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; --- --- Table structure for table `service` --- +-- -------------------------------------------------------- + +CREATE TABLE `page_views_daily` ( + `id` int NOT NULL AUTO_INCREMENT, + `page` varchar(191) NOT NULL, + `visit_date` date NOT NULL DEFAULT (curdate()), + `visit_count` int NOT NULL DEFAULT '1', + PRIMARY KEY (`id`), + UNIQUE KEY `unique_page_date` (`page`,`visit_date`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; + +-- -------------------------------------------------------- CREATE TABLE `service` ( - `id` int(11) NOT NULL, - `service_status` tinyint(1) NOT NULL + `id` int NOT NULL AUTO_INCREMENT, + `service_status` tinyint(1) NOT NULL, + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; --- --- Table structure for table `settings` --- +-- -------------------------------------------------------- CREATE TABLE `settings` ( - `id` int(11) NOT NULL, - `is_active` tinyint(1) NOT NULL + `id` int NOT NULL AUTO_INCREMENT, + `is_active` tinyint(1) NOT NULL, + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; --- --- Indexes for tables --- - -ALTER TABLE `admin` - ADD PRIMARY KEY (`id`); - -ALTER TABLE `articles` - ADD PRIMARY KEY (`ID`); - -ALTER TABLE `service` - ADD PRIMARY KEY (`id`); - -ALTER TABLE `settings` - ADD PRIMARY KEY (`id`); - --- --- AUTO_INCREMENT for tables --- - -ALTER TABLE `admin` - MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; - -ALTER TABLE `articles` - MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT; +-- -------------------------------------------------------- -ALTER TABLE `settings` - MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1; +CREATE TABLE `visitors` ( + `id` int NOT NULL AUTO_INCREMENT, + `cookie_id` varchar(64) DEFAULT NULL, + `visit_count` int DEFAULT '1', + `first_visit` datetime DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `cookie_id` (`cookie_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; COMMIT; diff --git a/public/main.css b/public/CSS/main.css similarity index 100% rename from public/main.css rename to public/CSS/main.css diff --git a/public/single.css b/public/CSS/single.css similarity index 100% rename from public/single.css rename to public/CSS/single.css diff --git a/public/JS/popout.js b/public/JS/popout.js new file mode 100644 index 0000000..e69de29 diff --git a/public/error.html b/public/error.html index 81ed519..06c2f4c 100644 --- a/public/error.html +++ b/public/error.html @@ -6,7 +6,7 @@ Error page - + - + + + + @@ -34,7 +37,7 @@

Error page

Something went wrong.

Try to search what you have been looking for on main page:

- Link: Blog main page +

Link: Blog main page

diff --git a/public/error_404.php b/public/error_404.php index d0c8e59..567a3d2 100644 --- a/public/error_404.php +++ b/public/error_404.php @@ -3,12 +3,16 @@ session_start(); -include_once 'functions.php'; -require_once 'service.alg.php'; -include 'connect.php'; +include_once './handlers/functions.php'; +require_once './handlers/service.alg.php'; +require_once './handlers/cookies.php'; service(); +$page = 'error_404.php'; +cookie($page); + + ?> @@ -16,8 +20,10 @@ 404 error - - + + + + diff --git a/public/connect.php b/public/handlers/connect.php similarity index 76% rename from public/connect.php rename to public/handlers/connect.php index aaa1736..655b0af 100644 --- a/public/connect.php +++ b/public/handlers/connect.php @@ -3,7 +3,7 @@ $host = "localhost"; $db_user = "root"; - $db_password =""; + $db_password ="root"; $db_name = "blog"; \ No newline at end of file diff --git a/public/handlers/cookies.php b/public/handlers/cookies.php new file mode 100644 index 0000000..1da2cfc --- /dev/null +++ b/public/handlers/cookies.php @@ -0,0 +1,19 @@ + Blog '; + echo '

Blog

'; } // main @@ -11,14 +11,12 @@ function all_articles(): void{ require 'connect.php'; - $conn = @new mysqli($host, $db_user, $db_password, $db_name); - error_reporting(E_ALL); - ini_set('display_errors', 1); + //error_reporting(E_ALL); + //ini_set('display_errors', 1); if ($conn->connect_errno) { - //echo "Error: " . $conn->connect_error; - echo 'error'; + echo 'Error'; return; } else { @@ -56,7 +54,7 @@ function all_articles(): void{ } } else { - echo "0 results"; + echo "No results found."; } $conn->close(); @@ -65,13 +63,15 @@ function all_articles(): void{ //footer -function foot(): string{ +function foot(): void{ + + $date = date("Y"); - return ' + echo "
- © 2024 - Dominik-developer + © 2024 - {$date} Dominik-developer

Contact: www.blog@example.com -
'; + "; } diff --git a/public/handlers/index.php b/public/handlers/index.php new file mode 100644 index 0000000..b0159fd --- /dev/null +++ b/public/handlers/index.php @@ -0,0 +1,4 @@ +connect_error; echo 'Error'; - header('Location: error.html'); + header('Location: ./error.html'); } else { @@ -44,7 +44,7 @@ function service(): void //unset($_SESSION['status']); echo 'last error '; - echo('Location: error.html'); + echo('Location: ./error.html'); } } diff --git a/public/single.alg.php b/public/handlers/single.alg.php similarity index 94% rename from public/single.alg.php rename to public/handlers/single.alg.php index f71b7c7..1c5888a 100644 --- a/public/single.alg.php +++ b/public/handlers/single.alg.php @@ -9,7 +9,7 @@ function articles($restored_title): void { if ($conn->connect_errno) { header("HTTP/1.1 500 Internal Server Error"); - header("Location: error_404.php"); + header("Location: ./error_404.php"); exit(); } @@ -45,7 +45,7 @@ function articles($restored_title): void { '; } else { header("HTTP/1.1 404 Not Found"); - header("Location: error_404.php"); + header("Location: ./error_404.php"); exit(); } diff --git a/public/handlers/visits.alg.php b/public/handlers/visits.alg.php new file mode 100644 index 0000000..72a0900 --- /dev/null +++ b/public/handlers/visits.alg.php @@ -0,0 +1,47 @@ +connect_errno) { + die("Connection failed: " . $conn->connect_error); + } + + // Sprawdzamy, czy w tabeli visitors istnieje już taki rekord (unikalny cookie_id) + $query = "SELECT * FROM visitors WHERE cookie_id = ?"; + $stmt = $conn->prepare($query); + $stmt->bind_param('s', $cookie_id); // Łączymy parametr cookie_id + $stmt->execute(); + $result = $stmt->get_result(); + + // Jeśli użytkownik nie istnieje, dodajemy nowy rekord + if ($result->num_rows === 0) { + $query = "INSERT INTO visitors (cookie_id, visit_count, first_visit) + VALUES (?, 1, CURRENT_TIMESTAMP)"; + $stmt = $conn->prepare($query); + $stmt->bind_param('s', $cookie_id); + $stmt->execute(); + } else { + // Jeśli już istnieje, aktualizujemy licznik odwiedzin + $query = "UPDATE visitors SET visit_count = visit_count + 1 + WHERE cookie_id = ?"; + $stmt = $conn->prepare($query); + $stmt->bind_param('s', $cookie_id); + $stmt->execute(); + } + + // Teraz aktualizujemy liczbę odwiedzin na stronie + $query = "INSERT INTO page_views_daily (page, visit_date, visit_count) + VALUES (?, CURRENT_DATE, 1) + ON DUPLICATE KEY UPDATE visit_count = visit_count + 1"; + $stmt = $conn->prepare($query); + $stmt->bind_param('s', $page); // Łączymy parametr strony + $stmt->execute(); + + $stmt->close(); +} +?> + diff --git a/public/index.php b/public/index.php index be04767..668d195 100644 --- a/public/index.php +++ b/public/index.php @@ -2,7 +2,7 @@ session_start(); -require_once 'service.alg.php'; +require_once './handlers/service.alg.php'; service(); diff --git a/public/main.php b/public/main.php index 22c7a54..e1eff6b 100644 --- a/public/main.php +++ b/public/main.php @@ -3,11 +3,15 @@ session_start(); -include_once 'functions.php'; -require_once 'service.alg.php'; +include './handlers/functions.php'; +require_once './handlers/service.alg.php'; +require_once './handlers/cookies.php'; service(); +$page = 'main.php'; +cookie($page); + ?> @@ -15,7 +19,9 @@ Blog Home Page - + + + diff --git a/public/service.html b/public/service.html index 502fbd8..eb593e5 100644 --- a/public/service.html +++ b/public/service.html @@ -11,7 +11,7 @@ - + - + + + + diff --git a/public/single.php b/public/single.php index 267eed8..806ee0d 100644 --- a/public/single.php +++ b/public/single.php @@ -3,10 +3,10 @@ session_start(); -include_once 'functions.php'; -require_once 'service.alg.php'; -include_once 'single.alg.php'; -include 'connect.php'; +include './handlers/functions.php'; +require_once './handlers/service.alg.php'; +require_once './handlers/cookies.php'; +include_once './handlers/single.alg.php'; service(); @@ -16,6 +16,10 @@ exit(); } +//$page = 'single.php?title='.$_GET['title']; +$page = 'single.php'; +cookie($page); + $restored_title = str_replace('_', ' ', filter_var($_GET['title'], FILTER_SANITIZE_SPECIAL_CHARS)); //optionally use - insted of _ ?> @@ -25,8 +29,10 @@ <?php echo htmlspecialchars($restored_title); ?> - - + + + + From 1e346d7779b63e411495e619e57dfc90d2161201 Mon Sep 17 00:00:00 2001 From: Dominik <163560221+Dominik-developer@users.noreply.github.com> Date: Tue, 11 Mar 2025 22:20:09 +0100 Subject: [PATCH 2/3] Cookies done --- CHANGELOG.md | 1 + HOW_TO_RUN.md | 6 ++--- public/CSS/popout.css | 41 +++++++++++++++++++++++++++++++++++ public/JS/popout.js | 39 +++++++++++++++++++++++++++++++++ public/error_404.php | 4 ++++ public/handlers/cookies.php | 20 +++++++++-------- public/handlers/functions.php | 11 ++++++++++ public/main.php | 6 +++++ public/single.php | 4 ++++ 9 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 public/CSS/popout.css diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b08597..d7d22a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ The project is ready for use but is still evolving. Work is ongoing to fix bugs 1 - cookies for users, db collects data for analytics 2 - tables for cookies added 3 - JS folder +4 - popout for cookies ..... ### Changed diff --git a/HOW_TO_RUN.md b/HOW_TO_RUN.md index d7890b3..6575289 100644 --- a/HOW_TO_RUN.md +++ b/HOW_TO_RUN.md @@ -8,9 +8,9 @@ CorelyPHP is a ready-to-deploy blogging platform designed for efficient web deve Before you begin, ensure you have the following installed: -- PHP (>= 8.0) -- MySQL (or another compatible database) -- XAMPP +- PHP (>= 8.0) +- MySQL (or another compatible database) +- XAMPP / MAMP - Git ## Installation diff --git a/public/CSS/popout.css b/public/CSS/popout.css new file mode 100644 index 0000000..1b8da71 --- /dev/null +++ b/public/CSS/popout.css @@ -0,0 +1,41 @@ +.cookie-popup { + position: fixed; + bottom: 20px; + left: 50%; + transform: translateX(-50%); + background-color: #333; + color: #fff; + padding: 15px 20px; + border-radius: 8px; + box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); + display: none; +} + +.cookie-popup a { + color: #fff; + text-decoration: underline; +} + +.cookie-popup button { + background-color: #fff; + color: #333; + border: none; + padding: 8px 12px; + margin-left: 10px; + cursor: pointer; + border-radius: 5px; + font-weight: bold; +} + +.cookie-popup button:hover { + background-color: #ddd; +} + +.cookie-popup button#reject-cookies { + background-color: #ff4d4d; /* Czerwony dla opcji odrzucenia */ + color: white; +} + +.cookie-popup button#reject-cookies:hover { + background-color: #cc0000 +} \ No newline at end of file diff --git a/public/JS/popout.js b/public/JS/popout.js index e69de29..0119f42 100644 --- a/public/JS/popout.js +++ b/public/JS/popout.js @@ -0,0 +1,39 @@ + +document.addEventListener("DOMContentLoaded", function () { + const popup = document.getElementById("cookie-popup"); + const acceptButton = document.getElementById("accept-cookies"); + const rejectButton = document.getElementById("reject-cookies"); + + const cookiesAccepted = document.cookie.includes("cookiesAccepted=true"); + const cookiesRejected = document.cookie.includes("cookiesAccepted=false"); + + if (!cookiesAccepted && !cookiesRejected) { + popup.style.display = "block"; + } + + function setCookie(name, value, days) { + let expires = ""; + if (days) { + const date = new Date(); + date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); + expires = "; expires=" + date.toUTCString(); + } + document.cookie = name + "=" + value + expires + "; path=/server/CorelyPHP-1.1.0/public/"; + } + + // Obsługa zgody na ciasteczka + acceptButton.addEventListener("click", function () { + setCookie("cookiesAccepted", "true", 365); + popup.style.display = "none"; + location.reload(); + }); + + // Obsługa odrzucenia ciasteczek + rejectButton.addEventListener("click", function () { + setCookie("cookiesAccepted", "false", 365); + popup.style.display = "none"; + location.reload(); + }); +}); + + diff --git a/public/error_404.php b/public/error_404.php index 567a3d2..3c4cee2 100644 --- a/public/error_404.php +++ b/public/error_404.php @@ -59,5 +59,9 @@ ?> + + diff --git a/public/handlers/cookies.php b/public/handlers/cookies.php index 1da2cfc..60214ed 100644 --- a/public/handlers/cookies.php +++ b/public/handlers/cookies.php @@ -3,17 +3,19 @@ require 'visits.alg.php'; function cookie($page): void { + if (isset($_COOKIE['cookiesAccepted']) && $_COOKIE['cookiesAccepted'] === 'true') { + setcookie("cookiesAccepted", "true", time() + (3600 * 24 * 365), "/server/CorelyPHP-1.1.0/public/"); - if (!isset($_COOKIE['visitor_id'])) { + if (!isset($_COOKIE['visitor_id'])) { + $cookie_id = bin2hex(random_bytes(16)); + setcookie('visitor_id', $cookie_id, time() + (3600 * 24 * 365), "/server/CorelyPHP-1.1.0/public/"); + } else { + $cookie_id = $_COOKIE['visitor_id']; + } - $cookie_id = bin2hex(random_bytes(16)); - setcookie('visitor_id', $cookie_id, time() + (3600 * 24 * 365), "/CorelyPHP-1.1.0/public/"); // path may need to be changed + updateVisitCount($cookie_id, $page); } else { - $cookie_id = $_COOKIE['visitor_id']; + setcookie('visitor_id', "", time() - 3600, "/server/CorelyPHP-1.1.0/public/"); } - - updateVisitCount($cookie_id, $page); - } - - //$page = basename($_SERVER['PHP_SELF']); \ No newline at end of file + \ No newline at end of file diff --git a/public/handlers/functions.php b/public/handlers/functions.php index 4cc9297..de34bf8 100644 --- a/public/handlers/functions.php +++ b/public/handlers/functions.php @@ -75,3 +75,14 @@ function foot(): void{ "; } + +function cookie_popout(): void { + + echo ' + diff --git a/public/single.php b/public/single.php index 806ee0d..e19bb97 100644 --- a/public/single.php +++ b/public/single.php @@ -64,5 +64,9 @@ ?> + + From b70c8a6a4a7ccc75161f738c27660a5a97cf1ec8 Mon Sep 17 00:00:00 2001 From: Dominik <163560221+Dominik-developer@users.noreply.github.com> Date: Tue, 11 Mar 2025 21:31:04 +0000 Subject: [PATCH 3/3] Fix --- public/JS/popout.js | 2 +- public/handlers/cookies.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/public/JS/popout.js b/public/JS/popout.js index 0119f42..4e03a48 100644 --- a/public/JS/popout.js +++ b/public/JS/popout.js @@ -18,7 +18,7 @@ document.addEventListener("DOMContentLoaded", function () { date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); expires = "; expires=" + date.toUTCString(); } - document.cookie = name + "=" + value + expires + "; path=/server/CorelyPHP-1.1.0/public/"; + document.cookie = name + "=" + value + expires + "; path=/public/"; //path may need to be changed for client } // Obsługa zgody na ciasteczka diff --git a/public/handlers/cookies.php b/public/handlers/cookies.php index 60214ed..0a0f284 100644 --- a/public/handlers/cookies.php +++ b/public/handlers/cookies.php @@ -4,18 +4,18 @@ function cookie($page): void { if (isset($_COOKIE['cookiesAccepted']) && $_COOKIE['cookiesAccepted'] === 'true') { - setcookie("cookiesAccepted", "true", time() + (3600 * 24 * 365), "/server/CorelyPHP-1.1.0/public/"); + setcookie("cookiesAccepted", "true", time() + (3600 * 24 * 365), "/public/"); //path may need to be changed for client if (!isset($_COOKIE['visitor_id'])) { $cookie_id = bin2hex(random_bytes(16)); - setcookie('visitor_id', $cookie_id, time() + (3600 * 24 * 365), "/server/CorelyPHP-1.1.0/public/"); + setcookie('visitor_id', $cookie_id, time() + (3600 * 24 * 365), "/public/"); } else { $cookie_id = $_COOKIE['visitor_id']; } updateVisitCount($cookie_id, $page); } else { - setcookie('visitor_id', "", time() - 3600, "/server/CorelyPHP-1.1.0/public/"); + setcookie('visitor_id', "", time() - 3600, "/public/"); } } \ No newline at end of file