We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 31187
    • 1 Posts
    Hi guys,

    It has been a while since I read something about the problem that there’s no page hit counter for MODx. Now I have tried to create one. I do not know if there’s already a counter for MODx, or not. So if there is still interest in a page hit counter for Modx, let me know in this thread. I will then add the entire source code here. smiley

    See you!
      • 33968
      • 863 Posts
      Hey, why not drop it in anyway tongue
        • 31187
        • 1 Posts
        Alright: you win. I must admit that the counter is not fully developed. However, it does a few things:

        - Count Visitors (via IP).
        - Count the calls without taking account of the IP.
        - Count all current calls.
        - Count all today’s visitors.
        - Count all visitors who are online (IPs the past 5 minutes)

        I would appreciate it in any case, that you’ll share your improvement with the community. For example, the counter is still missing the opportunity to collect data for each side. I had to translate it into English. So I hope everything is working fine.

        Config Code (getcount.config.php):
        <?php
        mysql_connect(
          "",        // Host
          "",        // User
          ""         // Password
        ) or die("Unable to connect to database server!");
        
        // The database name must be specified!
        mysql_select_db("") or die("The database is not available!");
        ?> 


        Installation Code:
        <?php
        include("getcount.config.php");
        
        $sql = "CREATE TABLE `modx_counter_ip` (
        `ip` VARCHAR( 24 ) NOT NULL PRIMARY KEY ,
        `time` INT NOT NULL 
        ) ENGINE = MYISAM ;
        ";
        if (mysql_query($sql)) {
         echo "<p>table `modx_counter_ip` created.</p>";
        }
        
        $sql = "CREATE TABLE `modx_counter_stats` (
        `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
        `date` VARCHAR( 10 ) NOT NULL ,
        `visitor` INT NOT NULL ,
        `hits` INT NOT NULL ,
         UNIQUE (
         `date`
         )
        ) ENGINE = MYISAM ;
        ";
        if (mysql_query($sql)) {
         echo "<p>table `modx_counter_stats` created.</p>";
        }
        ?> 


        getCount Snippet:
        <?php
        
        include("getcount.config.php");
        
        $ip = $_SERVER["REMOTE_ADDR"];
        $date = date("d.m.Y");
        
        // Delete old IP address
        $runningtime = time() - 300; // 300 seconds (= 5min.) IP Lock!
        mysql_query("DELETE FROM `modx_counter_ip` WHERE `time` <= " . $runningtime . "");
        
        // If the IP address is already in the table
        if (!mysql_fetch_row(mysql_query("SELECT `ip` FROM `modx_counter_ip` WHERE `ip` = '" . $ip . "'"))) {
         // No, then register IP address and timestamp
         mysql_query("INSERT INTO `modx_counter_ip` (`ip`,`time`) VALUES ('" . $ip . "','" . time() . "')");
        
         // Update Counter
         mysql_unbuffered_query("UPDATE `modx_counter_stats` SET `visitor` = `visitor` + 1 WHERE `date` = '" . $date . "'");
         if (mysql_affected_rows()==0) {
          mysql_query("INSERT INTO `modx_counter_stats` (`date`,`visitor`) VALUES ('" . $date . "','1');");
         }
        }
        
        // Update hits
        mysql_unbuffered_query("UPDATE `modx_counter_stats` SET `hits` = `hits` + 1 WHERE `date` = '" . $date . "'");
        
        // Todays Counter Data
        list($visitor_today, $hits_today) = mysql_fetch_row(mysql_query("SELECT `visitor`,`hits` FROM `modx_counter_stats` WHERE `date` = '" . $date . "'"));
        
        // Counter Data
        list($visitor_total, $hits_total) = mysql_fetch_row(mysql_query("SELECT sum(`visitor`), sum(`hits`) FROM `modx_counter_stats` WHERE 1"));
        
        // Visitors Online
        list($visitor_online) = mysql_fetch_row(mysql_query("SELECT COUNT(*) AS `ip` FROM `modx_counter_ip`"));
        
        // Output
        if ($userOnline == 1) {
        echo 'Visitors online: ' . $visitor_online . '';
        }
        if ($userToday == 1) {
        echo 'Visitors today: ' . number_format($visitor_today, 0, ",", ".") . '';
        }
        if ($user == 1) {
        echo'Visitors total: ' . number_format($visitor_total, 0, ",", ".") . '';
        }
        if ($hitsToday == 1) {
        echo ' Hits today: ' . number_format($hits_today, 0, ",", ".") . '';
        }
        if ($hits == 1) {
        echo ' Hits total: ' . number_format($hits_total, 0, ",", ".") . '';
        }


        So if you run the snippet [[!getCount?]] you’ll see no output but it’s counting. With [[!getCount? &hits=`1` &hitsToday=`1` &user=`1` &userToday=`1` &userOnline=`1`]] you’ll get all of the output ... I know this isn’t the best one but I think we can improve this a lot smiley.
          • 8872
          • 41 Posts
          I like this. Any plan to make a full extra?
            • 4855
            • 28 Posts
            how to install this extras?
            can this count the hits for each resources?so we can use its to display the most popular documents.