PHP Dynamic Qr code - Generator of dynamic and static Qr code V2.0
- Documentation - Version 1.0 / Created: 24/08/2020 Last Update: 16/10/2023 -Technical documentation deprecated as the core of the project has been modified in version 2.0
+PHP Dynamic Qr code - v2.x
+ Documentation - v2.x / Last Update: 16/10/2023 +What is a static qr code
A Static QR code has a fixed destination URL.
The information encoded in static QR codes are fixed. The destination/data is stored directly in the QR Code graphic (hence QR codes are also getting larger and form a more complex pattern if there is more data to store). Therefore, the stored destination/data cannot be changed. Any typo in an already printed QR code cannot be changed, as with any other printed material. Through this script you can create 13 different types of static qr code.
Installation
-Follow these steps to install the script:
+Setup
+You can choose between local installation (recommended for development) and docker installation (recommended for general use)
+Follow these steps to install the script locally:
- Clone the project from github -
- Copy /config/environment.php.example over to /config/environment.php -
- Edit the yaml file for docker or manually enter the credentials you want in the /config/environment.php file -
- Launch the setup file in your browser /install/setup.php +
- Edit config/environment.php with your database credential +
- Launch install/setup.php file in your browser
- Finish -
The script checks the requirements, write permissions on the generated folders and creates a database with sample data
If all went well you will see a summary screen with a login button. Alternatively, you will get an error message
+If all went well you will see a summary screen with a login button (username: superadmin, password: superadmin). Alternatively, you will get an error message
+Follow these steps to install the script with docker via docker compose (simplest method, recommended):
+-
+
- Download docker-compose.yml file from github and edit if you want (recommended) +
- docker compose build --no-cache && docker compose up -d +
- Open your browser at http://localhost:80 and login with (username: superadmin, password: superadmin) +
- Finish +
Follow these steps to install the script with docker via locally build image:
+-
+
- Clone the project from github +
- Clone the database project from github: https://github.com/giandonatoinverso/PHP-Dynamic-Qr-code-db +
- Build and run both docker image: docker buildx build -f Dockerfile +
- Open your browser at http://localhost:80 and login with (username: superadmin, password: superadmin) +
- Finish +
Upgrade
+To upgrade the program from version 1.0 to version 2.x and maintain backward compatibility with the QR codes you have already created, files and users, you can follow these steps:
+-
+
- Clone the project from github +
- Edit config/environment.php with your database credential +
- Launch upgrade/upgrade.php file in your browser +
- Finish +
The script makes the necessary changes in the database that adapt to the new source code.
+Multi account
+Starting from version 2.x, the functionality that supports the creation and management of QR codes with the support of multiple accounts has been added
+-
+
- Superadmin accounts can manage all users, all qrcodes created +
- Admin accounts can only manage QR codes created by them and "universal" ones, i.e. not associated with any specific account
+ + d) Open source libraries used +
-
+
- + MysqliDb Simple MySQLi wrapper and object mapper with prepared statements + +
- + vCard Qr code This class allows to easily generate a simple QR code using vCard 4.0 + +
- + PHP to ICS A convenient script to generate iCalendar (.ics) files on the fly in PHP + +
Backend Files Structure
--
-
-
- config
-
-
-
- config.php -
- - forms
-
-
-
- static
-
-
-
- bitcoin.php -
- bookmark.php -
- country-code.html -
- email.php -
- event.php -
- location.php -
- paypal.php -
- phone.php -
- skype.php -
- sms.php -
- text.php -
- vcard.php -
- whatsapp.php -
- wifi.php -
- - dynamic_table.php -
- static_table.php -
- users_table.php -
- add_dynamic_form.php -
- add_static_form.php -
- edit_dynamic_form.php -
- edit_static_form.php -
- users_form.php -
- filters.php -
- logo.php -
- qrcode_options.php -
-
- - static
-
- helpers
-
-
-
- helpers.php -
- - includes
-
-
-
- head.php -
- footer.php -
- navbar.php -
- sidebar.php -
- search_order.php -
- auth_validate.php -
- flash_messages.php -
- - lib
-
-
-
- MysqliDb
-
-
-
- MysqliDb.php -
- - Dynamic_Qrcode
-
-
-
- Dynamic_Qrcode.php -
- - Static_Qrcode
-
-
-
- Static_Qrcode.php -
- - Users
-
-
-
- Users.php -
- - vCard
-
-
-
- vCard.php -
- - ICS
-
-
-
- ICS.php -
-
- - MysqliDb
-
- saved_qrcode -
- index.php -
- login.php -
- logout.php -
- authenticate.php -
- dynamic_qrcodes.php -
- static_qrcodes.php -
- admin_users.php -
- add_dynamic.php -
- add_static.php -
- add_admin.php -
- edit_dynamic.php -
- edit_static.php -
- edit_admin.php -
- delete_dynamic.php -
- delete_static.php -
- delete_users.php -
- - a) Open source libraries used -
-
-
- - MysqliDb Simple MySQLi wrapper and object mapper with prepared statements - -
- - vCard Qr code This class allows to easily generate a simple QR code using vCard 4.0 - -
- - PHP to ICS A convenient script to generate iCalendar (.ics) files on the fly in PHP - -
- -
Database Structure
- a) dynamic_qrcodes -
-
-
- - `id` -- int -- primary key -- auto increment - -
- - `filename` -- varchar - -
- - `format` -- varchar - -
- - `identifier` -- longtext - -
- - `link` -- varchar - -
- - `qrcode` -- varchar - -
- - `scan` -- varchar - -
- - `state` -- varchar -- default: enable - -
- - `created_by` -- int - -
- - `created-at` -- timestamp - -
- - b) static_qrcodes -
-
-
- - `id` -- int -- primary key -- auto increment - -
- - `filename` -- varchar - -
- - `format` -- varchar - -
- - `type` -- varchar - -
- - `content` -- mediumtext - -
- - `qrcode` -- varchar - -
- - `state` -- varchar - -
- - `state` -- varchar -- default: enable - -
- - `created_by` -- int - -
- - `created-at` -- timestamp - -
- - b) admin_accounts -
-
-
- - `id` -- int -- primary key -- auto increment - -
- - `user_name` -- varchar - -
- - `password` -- varchar - -
- - `series_id` -- varchar - -
- - `remember_token`-- mediumtext - -
- - `expires` -- datetime - -
- - `admin_type` -- varchar - -
-
In this script we use the open source MysqliDb library. It is a very large library, however this script only needs the simplest functions.
-
-The configuration file for the database (and more) is located in /config/config.php and it is included in all php files of this script. To interact with the database, therefore, it will be necessary to call the function: getDbInstance().
-
-MysqliDb class methods used:
-query() : A method to perform select query, return array. Contains the returned rows from the query, LINE 646
-getValue() : A convenient SELECT COLUMN function to get a single column value from one row, return array of values, LINE 787
-insert() : Insert method to add new row, LINE 818
-update() : Update query that need first where() method, LINE 905
-where() : This method allows you to specify multiple (method chaining optional) AND WHERE statements for SQL queries, LINE 969
-orWhere() : This method allows you to specify multiple (method chaining optional) OR WHERE statements for SQL queries, LINE 1012
-orderBy() : This method allows you to specify multiple (method chaining optional) ORDER BY statements for SQL queries, LINE 1229
-getLastError() : Method returns mysql error, LINE 2079
-paginate() : Pagination wrapper to get(), LINE 2401
-
-
-
+
Script Structure
- General
- Qr code and Admin users -
- dynamic_qrcodes.php, static_qrcodes.php, users.php allow you to view the relevant data +
- dynamic_qrcode.php, static_qrcode.php, user.php allow you to add, modify and delete the relevant data +
- The optimization work has produced a script consisting of modules (php pages) with a similar macro-structure to each other. All pages include the config.php file which in turn includes the helpers.php file and the MysqliDb class and the auth_validation.php file which takes care of verifying on each page that the user is logged in (read authentication types).
-
-
---/*This file is generated after the script installation wizard and contains some constants used throughout the script as well as database credentials.*/ - -//Get current page: - -define('CURRENT_PAGE', basename($_SERVER['REQUEST_URI'])); - - -//You can change the folder where the qr code will be saved: - -define('PATH', './saved_qrcode/'); - - -//The directory includes the full path needed by the server (Apache). You can test by printing it. - -define('DIRECTORY', BASE_PATH.'/saved_qrcode/'); - - -//You can change the page name for the redirect and the search parameter (the default is "id"): - -define('READ_PATH', $_SERVER['HTTP_HOST'].'/read.php?id='); - - -require_once BASE_PATH . '/lib/MysqliDb/MysqliDb.php'; -require_once BASE_PATH . '/helpers/helpers.php'; - -define('DB_HOST', "%HOST%"); -define('DB_USER', "%USER%"); -define('DB_PASSWORD', "%PASSWORD%"); -define('DB_NAME', "%DB_NAME%"); - - -/** - * Get instance of DB object - */ -function getDbInstance() { - return new MysqliDb(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); -} - -
As for the HTML structure, all pages start by including the head.php page which contains the entire head tag, the navbar.php page, the sidebar.php page.
There is a header with the page title and then the different main content for each page. Finally, each page includes the footer.php file with the required js scripts; some pages have additional js scripts needed
@@ -483,502 +234,27 @@ As specified at the beginning, the frontend part of this script is done with
The dashboard page has a separate structure, while the dynamic qr codes, static qr codes and users sections have a very similar structure to each other.
- List all page (the same for all 3 sections)
- ├─ *page_name*.php
- ├─ ├─ includes/flash_messages.php
- ├─ ├─ includes/filters.php
- ├─ ├─ forms/*name*_table.php
- The following shows which files are included in the management pages (add, edit, delete) of the 3 sections:
- Dynamic qr codes section
- ├─ add_dynamic.php
- ├─ ├─ forms/add_dynamic_form.php
- ├─ edit_dynamic.php
- ├─ ├─ forms/edit_dynamic_form.php
- ├─ delete_dynamic.php
- Static qr codes section
- ├─ add_static.php
- ├─ ├─ forms/add_static_form.php
- ├─ ├─ ├─ forms/static/*type*.php example: text.php
- ├─ ├─ ├─ ├─ forms/qrcode_options.php
- ├─ edit_static.php
- ├─ ├─ forms/edit_static_form.php
- ├─ delete_static.php
- Admin section
- ├─ add_admin.php
- ├─ ├─ forms/users_form.php
- ├─ edit_admin.php
- ├─ ├─ forms/users_form.php
- ├─ delete_user.php
-
+
+ The dashboard page has a separate structure, while the dynamic qr codes, static qr codes and users sections have a very similar structure to each other:
- +-
+
This script is designed as OOP as possible. Therefore, all QR code management operations are performed using classes and methods. There are 3 classes for 3 sections (dynamic, static, users).
-All 3 have the same macrostructure. Below there is the explanation of only the two classes (dynamic, static). Within the explanations for the classes I do not write the entire code but I limit myself only to indicate a clear reference.
-N.B. Some functions are not mentioned or explained in detail because there are adequate comments inside the class file.
This script is designed as OOP as possible. Therefore, all QR code handling operations are performed using classes and methods. There are 3 classes for 3 the three sections (dynamic qrcodes, static qrcodes, users) and dynamic qrcodes, static qrcodes share a single Qrcode class which contains some methods used by both classes
-How authentication works
-There are two levels of authentication: admin and superadmin. The difference between the two is that the admin level cannot add or delete qr code. It is enabled to consult and modify the QR code already created. At installation time there is only one default superadmin user that it is recommended to change.
-This script adopts a "simple" but secure authentication method also with the aid of COOKIES to memorize the user's access and two pages for login and logout are included, moreover, most of the operations take place through external functions located in the helpers.php file.
-Dynamic Qr code class
- a) private functions -
-
-
-
- collect()
- --
-
----- - - -Dynamic_Qrcodes.php - collect() --
--/*It is a function called by add() and by edit() which allows you to initially store common data for both insertion and modification. -For this class these data are: */ - -$data_to_db['filename']; -$data_to_db['created_at']; -$data_to_db['link']; - - -//the function returns an array with the results which will be expanded later by the add() and edit() functions with additional specific data: - -return $data_to_db; - -
- - - setOptions() - -
- - addLogo() - -
- - failure() - -
- - success() - -
- - info() - -
- b) public functions -
-
-
- - setOrderingValues() - -
-
- add()
- --
-
----- - - -Dynamic_Qrcodes.php - add() --
--//We collect all the data necessary for the database and to build the qr code in this array: - -$data_to_db - - -//We collect all the additional options for the qr code through the setOptions() method in this array: - -$options - - -/*If in the folder where we save the qr code (constant "DIRECTORY") there is no file with the same name (and format), -we proceed with the creation: */ - -if(!file_exists(...)) - - -//We take the generated qr code and we create the name of the complete file to be inserted into the saved qr code folder: - -$content = file_get_contents(...); -$filename = DIRECTORY.$data_to_db['filename'].'.'.$data_to_db['format']; -file_put_contents($filename, $content); - - -//We insert in the database all the fields collected in input: - -$db->insert('dynamic_qrcodes', $data_to_db); - -
-
- -
- edit()
- --
-
----- - - -Dynamic_Qrcodes.php - edit() --
--//Through $_GET we recover the id of the qr code and the name of the current file: - -$dynamic_id = htmlspecialchars($_GET['dynamic_id'], ENT_QUOTES, 'UTF-8'); -$old_filename = htmlspecialchars($_GET['filename'], ENT_QUOTES, 'UTF-8'); - - -//We recover from the database the image format of the qr code with id taken from input: - -$format - - -/*To update the database we take the essential data with the collect() method and the "state" field as input. -We also update the "qrcode" field with the name of the file taken from input but with the same format: */ - -$data_to_db - - -/*If in the folder where we save the qr code (constant "DIRECTORY") there is no file with the new name or old name (and format), -we proceed with the creation: */ - -if(!file_exists(...)) - - -//We find the row corresponding to the id taken in input with the where() method and update the row with the new data with the update() method: - -$db->where('id', $dynamic_id); -$db->update('dynamic_qrcodes', $data_to_db); - - -//Let's rename the qr code file previously created with the new name via PHP function rename(): - -rename($old, $new); - -
-
- -
- cancel()
- --
-
----- - - -Dynamic_Qrcodes.php - cancel() --
--//We verify that the logged in user is of type "super" otherwise we print an error message: - -if($_SESSION['admin_type']!='super') - $this->failure('You don\'t have permission to perform this action'); - - -//We recover from the database the image format of the qr code with id taken from input: - -$format - - -//We find the row corresponding to the id taken in input with the where() method and update the row with the new data with the delete() method: - -$db->where('id', $dynamic_id); -$db->delete('dynamic_qrcodes'); - - -//We delete from the folder the qr code that we want to delete through the php unlink() function: - -unlink($file); - -
-
-
+
Dynamic Qr code
+
The physical creation of a dynamic qr code is managed by a free external provider https://api.qrserver.com/
Static Qr code class
- a) private functions -
-
-
-
- collect()
- --
-
----- - - -Static_Qrcodes.php - collect() --
--/*The function has the same purpose as the analogue already seen previously. -However,a "link" is not stored in this class, but the "type" of the qr code is assigned in addition to the "filename" and "created_at" fields: -*/ - -$data_to_db['filename']; -$data_to_db['created_at']; - - -/*The type is assigned in the creation phase and can no longer be modified, in fact a check is made on $ _GET['type'] -to verify that the function has been called by add() in order to "capture" the type. -If it is not very clear, review the section of the entry concerning static QR codes */ - -if($_GET['type'] != NULL) - $data_to_db['type'] = htmlspecialchars($_GET['type']); - - -
- - - setOptions() - -
- - addLogo() - -
- - failure() - -
- - success() - -
- - info() - -
- - requiredFieldsError() - -
-
- add()
- --
-
----- - - -Static_Qrcodes.php - add() --
--/*The function is very similar to add() for dynamic qr code. -It naturally differs in the data type of the qr code, which must be encoded to be used in a query part of a URL: */ - -$sData = urlencode($sData); - - -/*We collect all the data necessary for the database and to build the qr code in this array -(in this class we also add the "content" that has been stored in the qr code): */ - -$data_to_db; -$data_to_db['content'] = $sContent; - - -//The rest is all the same as the add() seen above... - -
-
- b) public functions -
-
-
- - setOrderingValues() - -
-
- textQrcode()
- --
-
----- - - -Example - textQrcode() --
--public function textQrcode($text) - { - if($text != NULL){ - $this->sData = $text; - $this->sContent = 'Text: '.$text; - $this->add($this->sData, $this->sContent); - } - else - $this->requiredFieldsError(); - } - - -/*Each function has as parameter the data to be inserted in the qr code, some of which are mandatory depending on the type. - -A check is made on the mandatory data as if they were "NULL" it would mean that the user has not filled in the required field; -in this case it ends by printing a standard error message. - -Otherwise, proceed to assign to the variable (declared private) sData what will be stored in the qr code -(possibly accompanied by standard strings for the type of qr code requested). - -The sContent variable is assigned the content to be stored in the database, already in HTML format ready for printing in the table. - -The function add() is called (in this class it is declared private) which receives the data for the qr code -and the content to be stored in the database */ - -
- - - emailQrcode() - -
- - phoneQrcode() - -
- - smsQrcode() - -
- - whatsappQrcode() - -
- - skypeQrcode() - -
- - locationQrcode() - -
-
- eventQrcode()
- --
-
----- - - -Explanation of eventQrcode() --
--/*It is possible to create an "event" qr code which, if scanned, adds the desired event to the default calendar. -This is possible thanks to the .ics calendar format. -We use a very convenient external library that automatically generates a single string to be stored in the qr code: */ - -$ics = new ICS(array( - 'location' => $location, - 'description' => $description, - 'dtstart' => $start, - 'dtend' => $end, - 'summary' => $title, - 'url' => $url - )); - -$this->sData = $ics->to_string(); - -/*After checking the mandatory data, the ICS class receives the event data through the array and -exploits the concatenation between strings through the to_string() function to generate the unique string to be stored. */ - -
- -
- vcardQrcode()
- --
-
----- - - -Explanation of vcardQrcode() --
--//Operation similar to eventQrcode() through the aid of an external library to generate the vCard 4.0 string to be stored. - -$vcard = new vCard; - $vcard->name($fullname); - $vcard->nickName($nickname); - $vcard->email($email); - $vcard->url($website); - $vcard->cellPhone($phone); - $vcard->homePhone($home_phone); - $vcard->workPhone($work_phone); - $vcard->organization($company); - $vcard->role($role); - $vcard->categories($categories); - $vcard->note($note); - $vcard->photo($photo); - $vcard->address($address, $city, $postcode, $state); - $vcard->create(); - -$this->sData = $vcard->get(); - - -/*After checking the mandatory data, for each field a special function is called which concatenates all the information -in a single string. It is retrieved through the get() function. */ - -
- - - bookmarkQrcode() - -
- - wifiQrcode() - -
- - paypalQrcode() - -
- - bitcoinQrcode() - -
- - edit() same operation of the analogous function for dynamic qr code - -
- - cancel() same operation of the analogous function for dynamic qr code - -
-
- Methods for different types of static qr code:
-
+
Static Qr code
+
The physical creation of a static qr code is managed by a free external provider https://api.qrserver.com/ and previously is managed differently depending on the type
The dashboard is the main page of the script. In this section there is a counter of the total number of qr code in the database, and in particular also the number of dynamic and static ones. There is also a counter regarding the number of scans of the dynamic qr code.
The script offers 2 examples of dynamic charts created with chart.js
The first shows a weekly report of the number of qr codes created (dynamic and static), the second graph offers an overview of the number of scans of dynamic qr codes.
-
-
-
- Report of qr code -
- Chart -
---//The values are determined by counting the number of lines through the getValue library function: - -$db->getValue ("table_name", "count(*)"); - - -//A custom query is available for the value of the scans that sum all the values of the scan column: - -$db->query("SELECT sum(scan) FROM dynamic_qrcodes"); - -
-- --//For the first chart I select the "created_at" column (of the dynamic and static qr code tables) with a value between the current date and 7 days less: - -$db->query("select `created_at`, `scan` from "table_name" where `created_at` > curdate()-7;"); - - -/*I also select the "scan" column with the same criteria because I will need it for the second graph. -I scan the entire array and through a switch having as expression the substring of type YYYY-MM-DD of the results extracted from the database. -N.B. In our database we store the "created_at" field also with the date and time because it is useful for sorting the qr code: */ - -foreach ($result as $row) { - switch (substr($row['created_at'],0,10)){ - case - ... - } -} - -
---/*For the complete functioning of the chart.js library visit the link in the documentation. -The js codes of the graphs are found in the -
List All
-The structure of the code for the listing process is the same for all types of QR code. Below there is an example for dynamic qr codes.
- There is a main page, in this case dynamic_qrcodes.php, which contains the code to retrieve the data necessary for the table and an external page called search_order.php which implements functions for searching and sorting the qr code.
- The HTML code includes: the flash_message.php file for managing the various messages to be printed,
-the filters.php file for managing the search and sorting filters and the dynamic_table.php file with the code that creates the entire table.
-
-
-
---//We retrieve the database instance and define which columns to extract from the database for the table we need: - -$db = getDbInstance(); -$select = array('id', 'filename', 'identifier', 'link', 'qrcode', 'scan', 'created_at', 'updated_at'); - - -/*We define the parameters with which it is possible to sort the qr codes in the table. -N.B. The parameters for ascending and descending sorting are always included separately in addition to those chosen by us. */ - -$search_fields = array('filename', 'identifier', 'link'); -require_once BASE_PATH . '/includes/search_order.php'; - - -/*We use the variable $page to keep track of the page where we are. -By default the value is "1" and we need it because we can set an element limit for each page with a special library function: */ - -$page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?? 1; -$db->pageLimit = 15; - - -//We build the pagination with the elements extracted from the database through a convenient library function: - -$rows = $db->arraybuilder()->paginate('dynamic_qrcodes', $page, $select); -$total_pages = $db->totalPages; - -
---//We define the dimensions for each column. Example: - - -- - - - -//We print the entire array of results extracted from the database in the respective columns. Example: - - - -ID -Filename -Unique redirect identifier - ... -- -/*For cancellation, the modal uses an external page (in this case delete_dynamic.php) using a form within the main page -that includes two hidden input fields: filename, del_id. Before confirming the cancellation, a different message is shown depending on the type of QR code. -*/ - -//We conclude the end of printing the elements and the table body: - - - -- - - ... - -/*The script includes 3 operations: modification, cancellation, download of the qr code. All 3 have their own button but the modification operation -refers to an external link, the cancellation is managed through a "modal fade" within the same page, while the HTML "download" attribute -is simply used for the download: */ - - - - - - - - - - - -
How does the insertion of a dynamic qr code work?
-The physical creation of a dynamic qr code is managed by a free external provider https://api.qrserver.com/
-The main page dedicated to the insertion is add_dynamic.php the form HTML-tag is also declared on this page, however the input fields are separated in another page called add_dynamic_form.php for a matter of code reusability
--
-
---//Let's create an instance of the class for the dynamic Qr code we discussed earlier: - -require_once BASE_PATH . '/lib/Dynamic_Qrcode/Dynamic_Qrcode.php'; -$dynamic_qrcode = new Dynamic_Qrcode(); - - -//We verify that the form has been sent with the POST method and we call the add() function shown above: - -if ($_SERVER['REQUEST_METHOD'] === 'POST') - $dynamic_qrcode->add(); - - -
-
-
---/*The input fields are recognized by the class through the "name" attribute. For example, the underlying input creates the "colorpicker" tool -whose data will be sent and processed by the class with the name (attribute) "foreground" and the chosen value "value": */ - - - - -//Similarly, there are some types of selection inputs with different options. The input is sent with a "name" and with a "value": - - - - -
How does the insertion of a static qr code work?
-The physical creation of a static qr code is managed by a free external provider https://api.qrserver.com/
-The main page dedicated to the insertion is add_static.php Unlike the previous creation page, the one for static qr codes does not provide a single form HTML-tag but each type of static qr code has its own form. For the input fields there is a secondary page called add_static_form.php which contains the menu of the various types of qr code that can be created and for each card (for each type) another file is included with the individual input fields, for example text.php
--
-
---//Let's create an instance of the class for the static Qr code we discussed earlier: - -require_once BASE_PATH . '/lib/Static_Qrcode/Static_Qrcode.php'; -$static_qrcode = new Static_Qrcode(); - - -/*We verify that the form has been sent with the POST method and with a control switch we examine the type of qr code that you want to create -through $ _GET ['type'] present in the link after sending the corresponding form. Each type corresponds to a different class method -that sends as parameters the input data of the form with the POST method. Example with the type "text": */ - -if ($_SERVER['REQUEST_METHOD'] === 'POST'){ - switch($_GET['type']){ - case 'text': $static_qrcode->textQrcode($_POST['text']); break; - ... - } -} - - -
-
-
-- --/*Within this page there is a menu divided into tabs in which each contains a different form depending on the type of static qr code to create. -Example with "text" type: */ - -
-
-
-- - --/*Each page relating to a type of qr code has the same structure. In particular, it is characterized by a form which once sent provides the input data -to the main creation page with the type inside the link (?type=), the options to customize the qr code and the individual input fields different from -type to type. Example for "text" type: */ - -
How does the modification of a qr code work?
-The modification process is essentially the same for both types of QR code.
-For dynamic qr code is possible to change: the file name, the url and is possible to enable or disable the redirect when the qr code is scanned. It is not possible to change the image format of the QR code as well as its unique identifier.
-For static qr codes is only possible to change the file name, since by definition a static qr code cannot be changed in its content. The image format of the qr code cannot be changed.
-Below there is an example of the modification process for dynamic qr code, which I repeat is the same for both types.
-The main page dedicated to the edit is edit_dynamic.php the form HTML-tag is also declared on this page, however the input fields are separated in another page called edit_dynamic_form.php for a matter of code reusability.
-
-
-
---//Let's create an instance of the class for the Dynamic Qr code we discussed earlier - -require_once BASE_PATH . '/lib/Dynamic_Qrcode/Dynamic_Qrcode.php'; -$dynamic_qrcode = new Dynamic_Qrcode(); - - -/*We retrieve from the link the id of the qr code and the requested operation (edit). I chose to add this additional parameter -because from the qr code table it is possible to add different operations for each one. */ - -$dynamic_id = htmlspecialchars($_GET['dynamic_id'], ENT_QUOTES, 'UTF-8'); -$operation = htmlspecialchars($_GET['operation'], ENT_QUOTES, 'UTF-8'); - - -//We verify that the form has been sent with the POST method and we call the edit() function shown above: - -if ($_SERVER['REQUEST_METHOD'] === 'POST') - $dynamic_qrcode->edit(); - -//If the requested operation is edit, we retrieve the information needed to fill in the edit form through the id of the qr code: - -if ($edit){ - $db->where('id', $dynamic_id); - $dynamic_qrcode = $db->getOne('dynamic_qrcodes'); -} - -
-
-
---/*The input fields are similar throughout the script. What changes is the declaration of the "value" attribute. -In fact, the value is retrieved from the database only if the edit operation has been chosen: */ - - - -
How does the deletion of a qr code work?
-The deletion process is essentially the same for both types of QR code. It is a very simple process, here is an example for static qr codes.
-There is a single delete_dynamic.php delete page which is reached through a unique link for each element in the qr code table.
-
-
-
---//Let's create an instance of the class for the Static Qr code we discussed earlier: - -require_once BASE_PATH . '/lib/Static_Qrcode/Static_Qrcode.php'; -$static_qrcode = new Static_Qrcode(); - - -/*We verify that the form has been sent with the POST method and we call the cancel() function shown above. -To this function we send as parameters the id of the qr code and the name of the file */ - -if ($_SERVER['REQUEST_METHOD'] === 'POST') - $static_qrcode->cancel($_POST['del_id'], $_POST['filename']); - -
What happens when i scan a dynamic qr code?
-The dynamic QR code reading process is quite simple. There is a single read.php page that does all the operations. -By default this page is located outside the main directory, however with your modifications it is possible to change its location. Instead, it is easier to change the name of this page, taking care to change the name also in the classes where the qr code is generated +
When a dynamic QR code is scanned, the read.php page opens by default which increases the scan counter and takes care of carrying out the appropriate redirect. + + You can change the name of this page by remembering to change the constant defined in config.php + + You can also customize the redirect page and increase the timer
--
-
---//Each link is generated in the form /read.php?id=xxxxx, so we retrieve the present id: - -$get_id = htmlspecialchars($_GET['id'], ENT_QUOTES, 'UTF-8'); - - -//We retrieve the database instance and select the link associated with the unique id as well as its status (enabled or disabled): - -$db = getDbInstance(); -$state = $db->query("SELECT state FROM dynamic_qrcodes WHERE identifier='$get_id'"); -$link = $db->query("SELECT link FROM dynamic_qrcodes WHERE identifier='$get_id'"); - - -//We increase the scan field since the qr code has just been scanned: - -$update = $db->query("UPDATE dynamic_qrcodes SET scan=scan+1 WHERE identifier='$get_id'"); - - -/*If the qr code is enabled we proceed with a normal HTML redirect towards the associated link; otherwise we print a simple message -informing the user that the link is disabled: */ - -if($state[0]['state'] == 'enable'){ - echo ''; - echo 'Loading...'; -} -else - echo 'Disabled link'; - - -//N.B. Any custom pages to show during the redirect are not included in this script. - -
Updates
--
-
-
- Version 1.0 – September, 2020 (Initial release) - -
