From e895f6f1ff8ce8b3e0e2f3691947bc6e05a54407 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Thu, 4 Jan 2024 02:53:57 +0700 Subject: [PATCH 1/4] feat: using env variable for project --- .env.example | 14 ++++++++++++++ .gitignore | 1 + README.md | 33 ++++++++++++++++++++++++--------- admin.php | 7 +++++++ index.php | 7 +++++++ lib/config/config.php | 9 +++++---- lib/config/database.php | 10 +++++----- lib/config/sendmail.php | 11 +++++++---- 8 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..c201d5d --- /dev/null +++ b/.env.example @@ -0,0 +1,14 @@ +PATH_URL=/ +PATH_URL_IMG=/public/upload/images/ +PATH_URL_IMG_PRODUCT=/public/upload/products/ + +DB_HOST=db_server +DB_PORT=3306 +DB_USER=root +DB_PASS=root +DB_NAME=chikoi + +SMTP_HOST=smtp.gmail.com +SMTP_PORT=465 +SMTP_UNAME +SMTP_PWORD diff --git a/.gitignore b/.gitignore index 1569daf..fdb11c7 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ vendor docker/certs docker/logs .php-cs-fixer.cache +.env diff --git a/README.md b/README.md index af522bd..27a9610 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,11 @@ Support this project :stuck_out_tongue_winking_eye: :pray:

# 1. Configuration requirements - - - Version PHP 7.2 and above (-> 8.1) + - Web Server: Apache + - Version PHP >= 8.0 - OpenSSL PHP Extension + - Composer (Please install composer before running this project) + - MySQL >= 8.0 (or MariaDB >= 10.0) # 2. Technology - Pure PHP language @@ -55,7 +57,7 @@ This is the path to the database file for you to download: [`/admin/database/*** Create a new database on **PHPMyAdmin** at your server, then import the .sql file that you just downloaded. -# 5. Request appropriate edits +# 5. Request configuration After a clone my repository to the local computer, you need to edit some code to be able to connect to the database and help the site works. @@ -87,17 +89,28 @@ define('DB_PASS', ''); define('DB_NAME', 'chikoi'); ``` -### 5.2 Edit .htaccess +### 5.3 Edit .htaccess Change RewriteBase - Recommend the path that matches your host address. ------------------------------------------------ - -so we will have: +So we will have: ``` RewriteBase / ``` +----------------------------------------------- + +> **Note**: This applies to the case where your project is in a subfolder, and you want it accessible from a subpath URL. +> +>**EXAMPLE**: +>``` +>http://localhost/new-mvc-shop/ +>``` +> So we will have: +> ``` +> RewriteBase /new-mvc-shop/ +> ``` + ### 5.4 Edit SMTP Mail > The third thing: @@ -120,8 +133,8 @@ Path: [`/lib/config/sendmail.php`](https://github.com/TanHongIT/new-mvc-shop/tre # 6. Demo -1. Front-End: [http://tanhongit.epizy.com/new-mvc-shop/home](http://tanhongit.epizy.com/new-mvc-shop/home) -2. Back-End: [http://tanhongit.epizy.com/new-mvc-shop/admin.php](http://tanhongit.epizy.com/new-mvc-shop/admin.php) +1. Front-End: [https://chikoiquan.tanhongit.com](https://chikoiquan.tanhongit.com) +2. Back-End: [https://chikoiquan.tanhongit.com/admin.php](https://chikoiquan.tanhongit.com/admin.php) > **_Account login on Backend_** @@ -155,6 +168,8 @@ Admin: **Admin Manager Page** ![Image](https://imgur.com/xOpAmb4.png) +![Image](https://imgur.com/u8lXnsz.png) + ---------------------------------------------------------------------------------

diff --git a/admin.php b/admin.php index ea8a87c..734adbe 100644 --- a/admin.php +++ b/admin.php @@ -1,5 +1,12 @@ load(); + session_start(); require_once('lib/model.php'); diff --git a/index.php b/index.php index 361a8ba..7fbc9d3 100644 --- a/index.php +++ b/index.php @@ -5,6 +5,13 @@ * Github: https://github.com/TanHongIT */ +use Dotenv\Dotenv; + +require __DIR__ . '/vendor/autoload.php'; + +$dotenv = Dotenv::createImmutable(__DIR__); +$dotenv->load(); + session_start(); require_once('lib/model.php'); diff --git a/lib/config/config.php b/lib/config/config.php index f67558c..a81668a 100644 --- a/lib/config/config.php +++ b/lib/config/config.php @@ -1,6 +1,7 @@ Date: Thu, 4 Jan 2024 03:09:54 +0700 Subject: [PATCH 2/4] fix: clean code and remove redundant code --- admin/models/feedbacks.php | 1 - admin/models/users.php | 3 -- content/controllers/category/index.php | 8 ++-- content/controllers/comment/index.php | 40 ++++++++----------- content/controllers/feedback/index.php | 2 + content/controllers/forgot-password/index.php | 4 +- .../controllers/forgot-password/request.php | 1 - .../result-change-password.php | 1 - content/controllers/register/index.php | 1 - content/controllers/register/resend.php | 1 - content/views/comment/index.php | 4 +- content/views/feedback/result.php | 4 +- 12 files changed, 33 insertions(+), 37 deletions(-) diff --git a/admin/models/feedbacks.php b/admin/models/feedbacks.php index e760c2f..1711d88 100644 --- a/admin/models/feedbacks.php +++ b/admin/models/feedbacks.php @@ -60,7 +60,6 @@ function updateFeedback(): void function feedbackReplyMail($html, $email) { //sendmail - require 'vendor/autoload.php'; include 'lib/config/sendmail.php'; $mail = new PHPMailer(true); diff --git a/admin/models/users.php b/admin/models/users.php index 9e7c2f4..55595eb 100644 --- a/admin/models/users.php +++ b/admin/models/users.php @@ -69,7 +69,6 @@ function changePassword($id, $newpassword, $currentPassword) ]; save('users', $options); //sendmail - require 'vendor/autoload.php'; include 'lib/config/sendmail.php'; $mail = new PHPMailer(true); $user = getRecord('users', $id); @@ -169,7 +168,6 @@ function user_update() $user_edited = getRecord('users', $userId); if ($user_edited['user_email'] != $currentEmail) { //send mail - require 'vendor/autoload.php'; include 'lib/config/sendmail.php'; $email = $user_edited['user_email']; $mail = new PHPMailer(true); @@ -271,7 +269,6 @@ function user_add() save('users', $user_add); } //send mail - require 'vendor/autoload.php'; include 'lib/config/sendmail.php'; $mail = new PHPMailer(true); diff --git a/content/controllers/category/index.php b/content/controllers/category/index.php index fdd8b2c..62c319b 100644 --- a/content/controllers/category/index.php +++ b/content/controllers/category/index.php @@ -1,14 +1,16 @@ 'id, subcategory_name', 'order_by' => 'id ASC', diff --git a/content/controllers/comment/index.php b/content/controllers/comment/index.php index bad6798..de29a9e 100644 --- a/content/controllers/comment/index.php +++ b/content/controllers/comment/index.php @@ -2,32 +2,26 @@ if (!empty($_POST)) { global $userNav; + + $commentData = [ + 'id' => 0, + 'product_id' => intval($_POST['product_id']), + 'email' => escape($_POST['email']), + 'author' => escape($_POST['author']), + 'content' => escape($_POST['content']), + 'createDate' => gmdate('Y-m-d H:i:s', time() + 7 * 3600), + 'user_id' => intval($_POST['user_id']), + ]; + if (isset($userNav)) { - $comment_add = [ - 'id' => 0, - 'product_id' => intval($_POST['product_id']), - 'email' => escape($_POST['email']), - 'author' => escape($_POST['author']), - 'content' => escape($_POST['content']), - 'createDate' => gmdate('Y-m-d H:i:s', time() + 7 * 3600), - 'user_id' => intval($_POST['user_id']), - 'link_image' => escape($_POST['link_image']), - ]; - } else { - $comment_add = [ - 'id' => 0, - 'product_id' => intval($_POST['product_id']), - 'email' => escape($_POST['email']), - 'author' => escape($_POST['author']), - 'content' => escape($_POST['content']), - 'createDate' => gmdate('Y-m-d H:i:s', time() + 7 * 3600), - 'user_id' => intval($_POST['user_id']), - ]; + $commentData['link_image'] = escape($_POST['link_image']); } - save('comments', $comment_add); + + save('comments', $commentData); echo "

Done! Bạn đã ghi lời bình luận của bạn lại thành công !!
Hãy Trở lại sản phẩm hoặc Đến trang chủ
"; } require('content/views/comment/index.php'); + // $input = json_decode(file_get_contents('php://input'), true); // $productID = $input['productID']; @@ -37,7 +31,7 @@ // $author = $input['author']; // $userID = $input['email']; -// $comment_add = array( +// $commentData = array( // 'id' => 0, // 'product_id' => intval($productID), // 'email' => escape($email), @@ -47,7 +41,7 @@ // 'user_id' => intval($userID), // 'link_image' => escape($link_image) // ); -// save('comments', $comment_add); +// save('comments', $commentData); // $option = array('product_id' => intval($productID)); diff --git a/content/controllers/feedback/index.php b/content/controllers/feedback/index.php index ac8776a..c27e9c9 100644 --- a/content/controllers/feedback/index.php +++ b/content/controllers/feedback/index.php @@ -4,11 +4,13 @@ if (!empty($_POST)) { feedback_add(); } + if (isset($_GET['product_id'])) { $productId = intval($_GET['product_id']); } else { $productId = 0; } + $product = getRecord('products', $productId); if (isset($userNav)) { $user_action = getRecord('users', $userNav); diff --git a/content/controllers/forgot-password/index.php b/content/controllers/forgot-password/index.php index afa687b..0613cde 100644 --- a/content/controllers/forgot-password/index.php +++ b/content/controllers/forgot-password/index.php @@ -1 +1,3 @@ - Date: Thu, 4 Jan 2024 09:39:26 +0700 Subject: [PATCH 3/4] docs: update readme and config --- .env.example | 8 ++-- .php-cs-fixer.dist.php | 1 + README.md | 82 ++++++++++++++++++++++++++++------------- lib/config/config.php | 4 +- lib/config/database.php | 2 +- 5 files changed, 65 insertions(+), 32 deletions(-) diff --git a/.env.example b/.env.example index c201d5d..b0f435e 100644 --- a/.env.example +++ b/.env.example @@ -1,12 +1,14 @@ +# if you don't want to set the value you can remove the line or comment it out with a hash (#) + PATH_URL=/ -PATH_URL_IMG=/public/upload/images/ -PATH_URL_IMG_PRODUCT=/public/upload/products/ +PATH_URL_IMG=public/upload/images/ +PATH_URL_IMG_PRODUCT=public/upload/products/ DB_HOST=db_server DB_PORT=3306 DB_USER=root DB_PASS=root -DB_NAME=chikoi +DB_NAME=new_mvc_shop_db SMTP_HOST=smtp.gmail.com SMTP_PORT=465 diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 58121af..bda1b6d 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -1,4 +1,5 @@ in([ diff --git a/README.md b/README.md index 27a9610..a8d72e8 100644 --- a/README.md +++ b/README.md @@ -59,37 +59,62 @@ Create a new database on **PHPMyAdmin** at your server, then import the .sql fil # 5. Request configuration -After a clone my repository to the local computer, you need to edit some code to be able to connect to the database and help the site works. +Clone the project to your computer: -### 5.1 Edit Config +```bash +git clone https://github.com/tanhongit/new-mvc-shop.git +``` -You need to change the path in the '**config.php**' file to match the location of this source code on your server and must match the domain you registered. +Run composer install: -Path: [`/lib/config/config.php`](https://github.com/TanHongIT/new-mvc-shop/tree/master/lib/config) +```bash +composer install +``` -```php - **Note:** +> +> The path of the config file that is using these environment variables is located at: [`/lib/config/config.php`](https://github.com/TanHongIT/new-mvc-shop/tree/master/lib/config/config.php) -```php -define('DB_HOST', 'localhost'); -define('DB_USER', 'root'); -define('DB_PASS', ''); -define('DB_NAME', 'chikoi'); +## 5.2 Edit Connect Database + +You need to change the connection information and import sql file to the database after you have cloned my repository so that the website can work. + +This is the path to the sql file for you to import to your database: +[`/admin/database/***.sql`](https://github.com/TanHongIT/new-mvc-shop/tree/master/admin/database) + +And change the connection information to match your database in .env file: + +```dotenv +DB_HOST=db_server +DB_PORT=3306 +DB_USER=root +DB_PASS=root +DB_NAME=chikoi ``` -### 5.3 Edit .htaccess +> **Note:** +> +> The path of the database config file that is using these environment variables is located at: [`/lib/config/database.php`](https://github.com/TanHongIT/new-mvc-shop/tree/master/lib/config/database.php) + +## 5.3 Edit .htaccess Change RewriteBase - Recommend the path that matches your host address. @@ -116,11 +141,13 @@ RewriteBase / > The third thing: You need to change the information about **SMTP Mail** to be able to use some functions about user account authentication, change passwords, notify users, ... -```php -define('SMTP_HOST','smtp.gmail.com'); -define('SMTP_PORT','465'); -define('SMTP_UNAME','add_your_mail'); -define('SMTP_PWORD','add_your_application_password_from_your_mail'); +Update the following information in the **.env** file: + +```dotenv +SMTP_HOST=smtp.gmail.com +SMTP_PORT=465 +SMTP_UNAME=add_your_mail +SMTP_PWORD=add_your_application_password_from_your_mail ``` Change the value of the constant **SMTP_UNAME** and **SMTP_PWORD** to match the configuration you added on your Gmail. @@ -129,7 +156,10 @@ Tips: https://support.google.com/accounts/answer/185833?hl=en **Where SMTP_PWORD is the application password for your _gmail.com_ account.** -Path: [`/lib/config/sendmail.php`](https://github.com/TanHongIT/new-mvc-shop/tree/master/lib/config) +> **Note:** +> +> The path of the email config file that is using these environment variables is located at: [`/lib/config/sendmail.php`](https://github.com/TanHongIT/new-mvc-shop/tree/master/lib/config/sendmail.php) + # 6. Demo diff --git a/lib/config/config.php b/lib/config/config.php index a81668a..b016cc6 100644 --- a/lib/config/config.php +++ b/lib/config/config.php @@ -2,6 +2,6 @@ define("PATH_URL", $_ENV['PATH_URL'] ?? '/'); -define("PATH_URL_IMG", $_ENV['PATH_URL_IMG'] ?? '/public/upload/images/'); +define("PATH_URL_IMG", PATH_URL . ($_ENV['PATH_URL_IMG'] ?? 'public/upload/images/')); -define("PATH_URL_IMG_PRODUCT", $_ENV['PATH_URL_IMG_PRODUCT'] ?? '/public/upload/products/'); +define("PATH_URL_IMG_PRODUCT", PATH_URL . ($_ENV['PATH_URL_IMG_PRODUCT'] ?? 'public/upload/product/')); diff --git a/lib/config/database.php b/lib/config/database.php index 7e9a47f..bc8b5eb 100644 --- a/lib/config/database.php +++ b/lib/config/database.php @@ -4,7 +4,7 @@ define("DB_PORT", $_ENV['DB_PORT'] ?? '3306'); define("DB_USER", $_ENV['DB_USER'] ?? 'root'); define("DB_PASS", $_ENV['DB_PASS'] ?? 'root'); -define("DB_NAME", $_ENV['DB_NAME'] ?? 'chikoi'); +define("DB_NAME", $_ENV['DB_NAME'] ?? 'new_mvc_shop_db'); if (isset($_SESSION['user'])) { $userNav = $_SESSION['user']['id']; From 0bf10acf85939f9908b4f28bf17671bca993a1e6 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Fri, 5 Jan 2024 10:07:19 +0700 Subject: [PATCH 4/4] fix: clean code and update env info --- .env.example | 17 +++++++++++++++++ docker-compose.yml | 2 +- lib/counter.php | 26 ++++++++++++++++++-------- lib/statistics.php | 38 ++++++++++++++++++++++++++++++-------- 4 files changed, 66 insertions(+), 17 deletions(-) diff --git a/.env.example b/.env.example index b0f435e..87da24e 100644 --- a/.env.example +++ b/.env.example @@ -14,3 +14,20 @@ SMTP_HOST=smtp.gmail.com SMTP_PORT=465 SMTP_UNAME SMTP_PWORD + +################### For docker ################### +PHP_VERSION_SELECTED=8.2 + +APP_NAME + +APP_PORT +SSL_PORT + +MYSQL_PORT +MYSQL_USER +MYSQL_ROOT_PASS +MYSQL_DB +MYSQL_PASS + +PHPMYADMIN_PORT +PHPMYADMIN_UPLOAD_LIMIT diff --git a/docker-compose.yml b/docker-compose.yml index b154b02..8952f5c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -34,7 +34,7 @@ services: container_name: '${APP_NAME:-new_mvc_shop}-db' restart: always ports: - - '${DB_PORT:-13306}:3306' + - '${MYSQL_PORT:-13306}:3306' environment: MYSQL_ROOT_PASSWORD: '${MYSQL_ROOT_PASS:-root}' MYSQL_USER: '${MYSQL_USER:-root}' diff --git a/lib/counter.php b/lib/counter.php index 47a39e8..9cb448a 100644 --- a/lib/counter.php +++ b/lib/counter.php @@ -6,7 +6,7 @@ //$time_check = $time - 30; //Ấn định thời gian là 10 phút -// function get_client_ip_env() +// function getClientIpEnv() // { // if (!empty($_SERVER['HTTP_CLIENT_IP'])) { // $ip = $_SERVER['HTTP_CLIENT_IP']; @@ -19,8 +19,13 @@ // } //https://www.virendrachandak.com/techtalk/getting-real-client-ip-address-in-php-2/ http://tanvietblog.com/2013/09/15/php-lay-dia-chi-ip-cua-khach-vieng-tham -// Function to get the client ip address -function get_client_ip_env() + +/** + * Function to get the client ip address + * + * @return bool|array|string + */ +function getClientIpEnv(): bool|array|string { if (getenv('HTTP_CLIENT_IP')) $ipaddress = getenv('HTTP_CLIENT_IP'); @@ -59,7 +64,8 @@ function get_client_ip_env() // return $ipaddress; // } -$ip = get_client_ip_env(); + +$ip = getClientIpEnv(); $browser = $_SERVER['HTTP_USER_AGENT']; $date = gmdate('Y-m-d H:i:s', time() + 7 * 3600); @@ -68,10 +74,14 @@ function get_client_ip_env() ]; $count = getTotal('users_online', $options); -if ($count == 0) { //Truy cập lần đầu - insert_user_online($session, $time, $ip, $browser, $date); -} else { //Truy cập lần 2 - update_user_online($session, $time, $ip, $browser, $date); +try { + if ($count == 0) { //Truy cập lần đầu + insert_user_online($session, $time, $ip, $browser, $date); + } else { //Truy cập lần 2 + update_user_online($session, $time, $ip, $browser, $date); + } +} catch (Exception $e) { + echo $e->getMessage(); } // $sql3 = "SELECT * FROM users_online"; diff --git a/lib/statistics.php b/lib/statistics.php index cf4e67f..7af5beb 100644 --- a/lib/statistics.php +++ b/lib/statistics.php @@ -1,13 +1,35 @@ close(); } -function update_user_online($session, $time, $ip, $browser, $date) + +/** + * @param $session + * @param $time + * @param $ip + * @param $browser + * @param $date + * + * @return void + * @throws Exception + */ +function update_user_online($session, $time, $ip, $browser, $date): void { - global $linkConnectDB; - $sql = "UPDATE users_online SET time='$time' ,ip='$ip', browser ='$browser', dateonline ='$date' WHERE session = '$session'"; - return mysqli_query($linkConnectDB, $sql); + $sql = "UPDATE users_online SET time='$time' , ip='$ip', browser ='$browser', dateonline ='$date' WHERE session = '$session'"; + $query = executeQuery($sql); + $query->close(); }