We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34929
    • 8 Posts
    Please help me to make "Page Hit Counter" snippet like this : One IP = One Viewed people.

    ps. Modx Evo

    global $table_prefix;
    
    if(!isset($_SESSION['usertype'])) { $_SESSION['usertype'] = ''; }
    
    if($modx->documentObject['donthit'] != 1 && $_SESSION['usertype'] != 'manager') {
      
      $sql = "INSERT INTO ".$table_prefix."page_hit_counter (page_id, page_count) VALUES ($modx->documentIdentifier, 1)
      ON DUPLICATE KEY UPDATE page_count=page_count+1";
      $modx->db->query($sql);
    
    }
    
    return;
    
      • 33968
      • 863 Posts
      That looks like an Evo snippet already. Where did you get the code from?
        • 34929
        • 8 Posts
        Quote from: lucas at Aug 14, 2011, 03:34 PM

        That looks like an Evo snippet already. Where did you get the code from?

        I know , that’s evo snippet. Ive got it from modx.com>extra>pagehitcount. It counts every entering, but I need snippet that will count one people as one view. In this snippet I can refresh and the counts will be rised, i don’t want it
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Ah, now I see. I wasn’t sure what exactly it was that you wanted. What you want is quite different from a simple page hit counter; this snippet would need to be modified to the point where it really won’t be the same snippet at all.

          Such a function will put a much heavier load on your database, and that table will become very large, since it’s recording visitors, not just your pages. If you have 100 pages being counted, that’s only 100 records in the table. But if you get 100 unique visitors a day, that’s 3,000 records being added to that table every month! That’s why Evo dropped the built-in visitor tracking feature years ago.

          I would strongly recommend using one of the analytics services, such as Google Analytics, for this, especially if your site is on shared hosting.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 34929
            • 8 Posts
            Thanks for the answer.

            You’re right that is a problem. But maybe make something with cookies. And it will count that visitor only by another browser.
            What about this?
              • 33968
              • 863 Posts
              Is this intended for front-end display (ie. show the hit count on the page) or for your own tracking purposes? If it’s for your own use you should definitely go with Google Analytics or something similar.
                • 34929
                • 8 Posts
                that is for front-end , for users. All counts will be in the most popular list
                  • 33968
                  • 863 Posts
                  Well, you definitely don’t want to be writing a record to the db for every visitor who comes to your site.

                  If it’s just yourself refreshing the page (and increasing the hit count) that you want to avoid, you could add a condition to the snippet to filter out hits from your ip address... or from admin users.

                  You also need to ask yourself if it’s actually a problem that people visiting a page more than once will register multiple times on the hit count. Take YouTube for example. I can watch a video 5 times and the hit count will go up 5 times, and that’s a good thing in my opinion...

                  Otherwise it seems like cookie or session might be the way to go.
                    • 34929
                    • 8 Posts
                    Could you help me with cookie or session
                      • 28984
                      • 6 Posts
                      Quote from: gigantz at Aug 15, 2011, 09:19 AM

                      Could you help me with cookie or session
                      On the fly, something like this will do the job:
                      <?php
                      if (isset($_COOKIE['hitcounter'])) {
                      	//do nothing.
                      } else {
                      	$host = explode('.', $_SERVER['HTTP_HOST']);
                      	$host = $host[sizeof($host) - 2].'.'.$host[sizeof($host) -1];
                      	if (isset($_SERVER['HTTP_REFERER'])) {
                      		$url_info = parse_url($_SERVER['HTTP_REFERER']);
                      	}
                      	$pageHitCounter = // take the value from the DB;
                      	setcookie('hitcounter', '1', time()+$lifeTime, $url_info['path'], '.'.$host); // $lifetime is the cookie life in miliseconds //
                      	$pageHitCounter++;
                      	// and save to DB the new $pageHitCounter value;
                      }
                      
                      


                      I can not step into details about retrieving and saving the value from and to the DB, because I’m a Revo user and have no idea how to do this in Evo. In Revo I usually use template variable in which I save the values...
                      Hope I’ve helped a little smiley
                        Apache 2.0.63 (MacOS X)
                        PHP Version 5.2.13
                        MySQL 5.1.44
                        MODx Revolution 2.0.8-pl (traditional)

                        Made By Many