Full content of the article:
Databases serve as vast repositories of data, playing a pivotal role in modern organizations. With the ever-increasing volume of data, the need for effective tools and methods to manage and analyze this data is more pressing than ever. One of the most important aspects of database management is measuring and analyzing visit statistics. These statistics allow us to answer the following questions:
Which part of the database receives the most visits?
What kind of data are users interested in?
What are users’ access patterns?
Is the system experiencing performance issues?
Answering these questions helps us improve system performance, increase user satisfaction, and make better decisions.
Types of Visit Statistics:
Database visit statistics can be categorized into various types, each providing valuable information. Some of the most important types include:
Total number of visits: The number of times the database has been accessed.
Number of unique visits: The number of distinct users who have accessed the database.
Average visit duration: The average amount of time each user spends on the database.
Most visited pages: The most popular pages or tables in the database.
Peak visit times: The hours and days with the highest traffic to the database.
Types of access devices: The devices used to access the database (e.g., computers, mobile phones, tablets).
Tools and Techniques for Collecting and Analyzing Visit Statistics:
Various tools and techniques can be used to collect and analyze database visit statistics. Some of the most common ones include:
Server logs: Recording all user activities in the database.
Web analytics tools: Tools like Google Analytics used to analyze web traffic.
Database management systems (DBMS): Many DBMSs have built-in tools for collecting statistics.
Structured Query Language (SQL): For extracting the desired data from the database.
Data visualization tools: For graphically displaying statistics and facilitating analysis.
Analyzing Database Visit Statistics Code:
Based on the above explanations, we are going to provide you with a code that contains information such as:
1- Daily visit statistics – Yesterday’s visit statistics – Weekly visit statistics – Monthly visit statistics
2- The number of online users on the site
**This code will retrieve data from the database and display these statistics to users using shortcodes.
At the end, we will provide the complete and tested code for you to download.
Starte:
First, create the following directory structure within your WordPress theme:
Your template path \templates\visit
Then, inside the visit
folder, create a new file named visit-online.php
.
Now, open the visit-online.php
file in your code editor and paste the following code inside it:
$time_zone = 3600; $today = date( "Y-m-d", time() + $time_zone ); $yesterday = date( "Y-m-d", time() - 86400 + $time_zone ); $week_visit = 0; $month_visit = 0; $month_str = null; $file_src = get_template_directory() . '/templates/visit/visit-statistics.txt'; chmod( $file_src, 0755 ); $read_file = file_get_contents( $file_src ); if ( filesize( $file_src ) > 0 || $read_file != '' ) { $split_file = explode( '|', $read_file ); $modify = $split_file[ 31 ]; if ( $modify != $today ) { $today_visit = 1; if ( $modify == $yesterday ) { $yesterday_visit = $split_file[ 0 ]; } else { $yesterday_visit = 0; } $week_visit += $today_visit + $yesterday_visit; $month_visit += $today_visit + $yesterday_visit; for ( $i = 2; $i < 30; $i++ ) { if (is_numeric($split_file[$i - 1])) { $month_str .= intval($split_file[ $i - 1 ]); } else { if (is_numeric($data)) { // If the data is numeric, return it as an integer return intval($data); } else { // If the data is non-numeric, check if it contains an integer if (preg_match('/(\d+)/', $data, $matches)) { // If an integer is found, return it return intval($matches[0]); } else { // Return 0 if integer not found return 0; } } } if ( $i < 29 ) { $month_str .= '|'; } if ( $i < 8 ) { $week_visit += intval($split_file[ $i - 1 ]); } $month_visit += intval($split_file[ $i - 1 ]); } $total_visit = 0; // Initialize with a default value (0 visits) if (is_numeric($split_file[30])) { $total_visit = $split_file[30]; } $total_visit += 1; // Add 1 to the total visit count $last_modify = $today; } else { $today_visit = intval($split_file[ 0 ] + 1); $yesterday_visit = intval($split_file[ 1 ]); $week_visit += $today_visit + $yesterday_visit; $month_visit += $today_visit + $yesterday_visit; for ( $i = 2; $i < 30; $i++ ) { if (is_numeric($split_file[$i])) { $month_str .= intval($split_file[ $i]); } else { if (is_numeric($data)) { // If the data is numeric, return it as an integer return intval($data); } else { // If the data is non-numeric, check if it contains an integer if (preg_match('/(\d+)/', $data, $matches)) { // If an integer is found, return it return intval($matches[0]); } else { // Return 0 if integer not found return 0; } } } if ( $i < 29 ) { $month_str .= '|'; } if ( $i < 8 ) { $week_visit += intval($split_file[ $i]); } $month_visit += intval($split_file[ $i ]); } $total_visit = intval($split_file[ 30 ] + 1); $last_modify = $today; } } else { $today_visit = 1; $yesterday_visit = 0; $week_visit = 1; $month_visit = 1; for ( $i = 2; $i < 30; $i++ ) { $month_str .= '0'; if ( $i < 29 ) { $month_str .= '|'; } } $total_visit = 1; $last_modify = $today; } $file_src_handle = fopen( $file_src, 'w+' ); $visit_data = $today_visit . '|' . $yesterday_visit . '|' . $month_str . '|' . $total_visit . '|' . $last_modify; fwrite( $file_src_handle, $visit_data ); fclose( $file_src_handle ); $config_array = array( 'user_time' => date( "YmdHis", time() + $time_zone ), 'user_ip' => $_SERVER[ 'REMOTE_ADDR' ], 'file_name' => get_template_directory() . '/templates/visit/visit-online.txt' ); chmod( $config_array[ 'file_name' ], 0755 ); $online_file = file_get_contents( $config_array[ 'file_name' ] ); $online_file = explode( "\r\n", $online_file ); foreach ( $online_file as $key => $value ) { if ( is_null( $value ) || $value == '' ) { unset( $online_file[ $key ] ); } } foreach ( $online_file as $key => $value ) { $user_ip_time = explode( "|", $value ); if ( $user_ip_time[ 1 ] <= date( "YmdHis", time() + $time_zone - 300 ) ) { unset( $online_file[ $key ] ); } if ( $user_ip_time[ 0 ] == $config_array[ 'user_ip' ] ) { unset( $online_file[ $key ] ); } } $online = 1; foreach ( $online_file as $online_users ) { $user_ip_time = explode( "|", $online_users ); if ( $user_ip_time[ 1 ] >= date( "YmdHis", time() + $time_zone - 300 ) ) { $online++; } } $new_online = $config_array[ 'user_ip' ] . "|" . $config_array[ 'user_time' ] . "\r\n"; foreach ( $online_file as $key => $value ) { $new_online .= $value . "\r\n"; } $file_src_handle = fopen( $config_array[ 'file_name' ], 'w+' ); fwrite( $file_src_handle, $new_online ); fclose( $file_src_handle );
$time_zone = 3600;:
This line defines a variable named $time_zone and assigns it a value of 3600 seconds (equivalent to one hour). This value is used to adjust the time zone. For determining your time zone, you can refer to article UTC Time Zone.
**
Next, by placing the code above, two files named visit-statistics.txt
and visit-online.txt
will be created. The retrieved visit data from the database will be printed in these files. These two files are our main sources of statistical information.
**
After completing the above steps, it’s time to get the output!
**First, place the following code in one of your template pages (for example: footer.php
):
<?php include_once( get_template_directory() . '/templates/visit/visit-online.php' ); ?>
Now, our code has been correctly added to the selected file. And to call statistics from the database, place the following shortcodes in your file:
**For today’s statistics:
<?php echo number_format($today_visit) ?>
For yesterday’s statistics:
<?php echo number_format($yesterday_visit) ?>
For weekly statistics:
<?php echo number_format($week_visit) ?>
For monthly statistics:
<?php echo number_format($month_visit) ?>
To display the number of online users on the site:
<?php echo number_format($online) ?>
**
Note: You can only use the written code on one of your template pages; therefore, if you want the statistics to be displayed on all pages, you should add the code to the header or footer of your template.
Comment
[php] Your code [/php]