Fetching database visit statistics using PHP without plugins

73

July 29, 2024

August 27, 2024

00:00:00

Today:Saturday

2024/06/05

  • home
  • Wordpress
  • Fetching database visit statistics using PHP without plugins

Estimated reading time: 12 minutes


Font Size

Excerpt from this article:

In the information age, databases serve as the heart of organizations, storing valuable data. A crucial aspect of database management is measuring and analyzing visit statistics. These statistics not only help us understand user behavior and access patterns but are also essential for optimizing system performance, identifying potential problems, and making future plans.

  • Supporting us, a step towards progress!

    • With your help, we can continue our journey of providing high-quality and valuable content. Your support, no matter how small, gives us the motivation and power to take firmer steps on this challenging path.


      Two simple ways to support us:
      1. Contribute to cryptocurrency mining: Share some of your system's processing power to help us mine cryptocurrencies.
      2.  Send cryptocurrency: You can directly send cryptocurrency to our wallet.


      With your support, a world of useful information will be available to everyone!

To get to know us better, check out our About Us page.

We dedicate countless hours and energy to creating high-quality, informative content for you. Your support is what motivates us to continue this journey.

We sincerely appreciate your support.

1,000,000 Hashes (Total) = 0.000,000,03 BTC

1 BTC BitTorrentBTTC

2 BTC BEP-20PEPE

3 BTC ShibaSHIB

4 BTC WINkLinkWIN

5 BTC RadiocacaRACA

6 BTC FlokiFLOKI

7 BTC EverdomeDOME

8 BTC DentDENT

9 BTC ReefREEF

10 BTC HoloHOT

11 BTC LeverFiLEVER

12 BTC AmpAMP

13 BTC Reserve rightsRSR

14 BTC AnkrANKR

15 BTC Beta FinanceBETA

16 BTC TRONTRX

17 BTC DogecoinDOGE

18 BTC TetherUSDT

19 BTC Chorono.techTIME

20 BTC CompoundCOMP

21 BTC BitcoinCashBCH

22 BTC BinanceCoinBNB

23 BTC EthereumETH

24 BTC BitcoinBTC

We sincerely appreciate your support. The provided address is defined on the TRC-20 network.

Please deposit BTTC only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit BTTC only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit PEPE only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit PEPE only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit SHIB only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit SHIB only to this address.

We sincerely appreciate your support. The provided address is defined on the TRC-20 network.

Please deposit WIN only to this address.

We sincerely appreciate your support. The provided address is defined on the TRC-20 network.

Please deposit WIN only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit RACA only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit RACA only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit FLOKI only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit FLOKI only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit DOME only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit DOME only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit DENT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit DENT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit REEF only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit REEF only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit HOT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit HOT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit LEVER only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit LEVER only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit AMP only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit AMP only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit RSR only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit RSR only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit ANKR only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit ANKR only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit BETA only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit BETA only to this address.

We sincerely appreciate your support. The provided address is defined on the TRON network.

Please deposit TRX only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit TRX only to this address.

We sincerely appreciate your support. The provided address is defined on the DOGECOIN network.

Please deposit DOGE only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit DOGE only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit USDT only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit USDT only to this address.

We sincerely appreciate your support. The provided address is defined on the TRC-20 network.

Please deposit USDT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit TIME only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit TIME only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit COMP only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit COMP only to this address.

We sincerely appreciate your support. The provided address is defined on the BITCOINCASH network.

Please deposit BCH only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit BCH only to this address.

We sincerely appreciate your support. The provided address is defined on the BSC network.

Please deposit BNB only to this address.

We sincerely appreciate your support. The provided address is defined on the BSC network.

Please deposit BNB only to this address.

We sincerely appreciate your support. The provided address is defined on the ETHEREUM network.

Please deposit ETH only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit ETH only to this address.

We sincerely appreciate your support. The provided address is defined on the BITCOIN network.

Please deposit BTC only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit BTC only to this address.

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.

You can download the complete and tested code from the following link:

Download

Comment

[php] Your code [/php]

Leave a Reply

Your email address will not be published.Required fields are marked *

  • All Our Articles

    • To provide you with an overview of all our published articles, we have created this section. In our articles, we will focus on the practical application of the material, rather than just filling the site with content. Therefore, our articles will be few but very practical. Our articles will cover a wide range of technology and programming topics.

UTC Time Zone
Read More
Fetching database visit statistics using PHP without plugins
Read More
Examination of title code within WordPress templates
Read More
Adding a Period to Categories in WordPress: A Simple and Effective Solution
Read More
Retrieving and Displaying a Complete List of WordPress Site Categories
Read More
Displaying Category Name and Post Count on Category Archive Page
Read More
Showing accurate post view counts on archive pages
Read More

Fonts made from Web Fonts is licensed by CC BY 4.0

13,135