• Hi,I found on a tutorial a code for a plugin that counts visits on the site,I put it on the server and something strange:90% of visits are counted twice.
    https://img585.imageshack.us/img585/3255/kgof.jpg

    On localhost tests,is seems to work fine,that is the code:

    <?php
    /*
    Plugin Name: Count visits
    Author: User
    Version: 1.0
     */
    
    function bdetector_activate()
    {
    	global $wpdb;
    
    	$table_name = $wpdb->prefix . 'bdetector';
    
    	// will return NULL if there isn't one
    	if ( $wpdb->get_var('SHOW TABLES LIKE ' . $table_name) != $table_name )
    	{
    		$sql = 'CREATE TABLE ' . $table_name . '(
    				id INTEGER(10) UNSIGNED AUTO_INCREMENT,
    				hit_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    				user_agent VARCHAR (255),
    				PRIMARY KEY  (id) )';
    
    		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    		dbDelta($sql);
    
    		add_option('bdetector_database_version','1.0');
    	}
    }
    
    register_activation_hook(__FILE__,'bdetector_activate');
    
    function bdetector_insert_useragent()
    {
    	global $wpdb;
    
    	$table_name = $wpdb->prefix . 'bdetector';
    
    	$wpdb->insert($table_name,array('user_agent'=>$_SERVER['REMOTE_ADDR']),array('%s'));
    }
    
    add_action('wp_footer','bdetector_insert_useragent');

    Thanks for any tip

Viewing 13 replies - 16 through 28 (of 28 total)
  • Thread Starter mrapi

    (@mrapi)

    I didn’t notice that you’re inserting IP addresses in the field meant for “User Agent”

    Try changing the action to init.

    If the problem still persists disable JavaScript on you browser and try accessing your site.

    Thread Starter mrapi

    (@mrapi)

    I’ve replaced wp_footer with init:

    //add_action('wp_footer','bdetector_insert_useragent');
      add_action('init','bdetector_insert_useragent');

    nothing changed,also the same thing with JavaScript disabled
    ??

    thanks.

    I tried this code on a fresh installation of WordPress 3.8 without any plugins on the Twenty Fourteen theme and I get only a single entry.

    Try switching to the Twenty Thirteen theme and see if it works.
    Also disable your plugins one by one.

    Thread Starter mrapi

    (@mrapi)

    Hi Jesin,indeed good points of debugging,tested but the same,
    I give up,I will use this plugin as it is,lot of time wasted also for you,
    thanks

    Moderator bcworkz

    (@bcworkz)

    Hi mrapi, I’ve been lurking on your curious problem. I just now looked at your posted “latest records” from yesterday. I assume the 117.193.97.115 hits are Jesin A doing his 3 test requests and the 31.22.4.26 is the mysterious duplicate record you are trying to resolve.

    31.22.4.26 is the IP for your server. This suggests your host has some sort of caching system in place that is the source of your mysterious duplicate hits. If so, you should be able alter your script to not store any records from the IPs of your server. Also reject 31.22.4.25 and 207.182.142.243 to start with, there may be more, but limited if this is indeed a caching scheme. This could cause you to miss an odd request from someone using the shared server as a proxy, but virtually all legitimate user requests will not come from host IP ranges.

    Thread Starter mrapi

    (@mrapi)

    Hi bcworks.
    31.22.4.26 is a part of the problem,also there seems to be lots of other IPs part of the problem,as in image

    https://imageshack.us/a/img802/4458/ph3c.jpg
    https://imageshack.us/a/img801/448/fm9t.jpg
    thanks

    Thread Starter mrapi

    (@mrapi)

    Now I’ve got one idea: I’ll try to use jQuery+cookie creation/detection then with ajax will send data to server

    Thread Starter mrapi

    (@mrapi)

    on localhost seems to work:
    I need just one opinion about the code:

    php file :

    <?php
    /*
    Plugin Name: Drew's Browser Detector Plugin
    Plugin URI: https://www.falkonproductions.com/browserDetector/
    Description: This plugin will store the user agents for later parsing and display
    Author: Drew Falkman
    Version: 1.0
    Author URI: https://www.falkonproductions.com/
     */
    
    function bdetector_activate()
    {
    	global $wpdb;
    
    	$table_name = $wpdb->prefix . 'bdetector';
    
    	// will return NULL if there isn't one
    	if ( $wpdb->get_var("SHOW TABLES LIKE '" . $table_name."'") != $table_name )
    	{
    		$sql = 'CREATE TABLE ' . $table_name . '(
    				id INTEGER(10) UNSIGNED AUTO_INCREMENT,
    				hit_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    				user_agent VARCHAR (255),
    				PRIMARY KEY  (id) )';
    
    		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    		dbDelta($sql);
    
    		add_option('bdetector_database_version','1.0');
    	}
    }
    
    register_activation_hook(__FILE__,'bdetector_activate');
    
    add_action('wp_enqueue_scripts', function()
      {
          wp_enqueue_script('ajax-script_cvisit','/wp-content/plugins/browser_detector/js/count_visits.js', array('jquery'), '1.0', true);
    
          wp_localize_script( 'ajax-script_cvisit', 'ajax_object_cv',
              array( 'ajax_url' => admin_url( 'admin-ajax.php' )) );
      });
    
    function fn_visit_AddtoCount()
    {
        if (!isset($_COOKIE['exps_visited']))
        {
            setcookie('exps_visited',1);
    
            global $wpdb;
    
            $table_name = $wpdb->prefix . 'bdetector';
            $wpdb->insert($table_name,array('user_agent'=>$_SERVER['REMOTE_ADDR'],
                    'hit_date'=>current_time( 'mysql' )
                ),
                array('%s','%s')
            );
    
        }
    
        die();
    }
    
    add_action('wp_ajax_fn_visit_AddtoCount', 'fn_visit_AddtoCount');
    add_action('wp_ajax_nopriv_fn_visit_AddtoCount', 'fn_visit_AddtoCount');

    js file :

    var $j = jQuery.noConflict();
    
    $j(document).ready( function()
      {
    
        $j.ajax(
          {
              type:"POST",
              url: ajax_object_cv.ajax_url,
              data: {
                  action: 'fn_visit_AddtoCount'
              },
              success:function(response)
              {
               //   alert('Got this from the server: ' + response);
              }
    
          });
    
     }
                           );

    also it is possible to use a function to get plugin directory instead of using this:
    /wp-content/plugins/browser_detector’…… ?

    thanks

    Thread Starter mrapi

    (@mrapi)

    Thanks, plugins_url() function it is ok.

    One question: I’ve tried to see cookie created on first visit (exps_visited) so with Chrome – right click on the page – Inspect element – Resources – Cookies …
    but is not listed there although is seem to work,visits on the page are counted once for current browser session.
    How can I see the cookie?
    Thanks.

    I don’t know about Chrome but in Firefox I use the firebug addon to view cookies and also LiveHTTPHeaders to see if the cookies are sent in HTTP Headers.

    See if this works for you https://www.wikihow.com/View-Cookies#Google_Chrome

    Thread Starter mrapi

    (@mrapi)

    I’ve tested all 3 alternatives:

    with link for chrome I could see the cookie
    with firebug I only see 2 items ?? :
    https://img191.imageshack.us/img191/3518/ri8g.jpg

    with LiveHTTPHeaders,lots of data,I’ve found it :

    POST /wordpress/wp-admin/admin-ajax.php HTTP/1.1
    User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:26.0) Gecko/20100101 Firefox/26.0
    Accept: */*
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    X-Requested-With: XMLHttpRequest
    Content-Length: 26
    Cookie: exps_visited=1; fc=fcVal=2019808097268502242; _ga=GA1.2.2017174040.1382182233
    DNT: 1

    Connection: keep-alive
    Pragma: no-cache
    Cache-Control: no-cache
    action=fn_visit_AddtoCount
    HTTP/1.1 200 OK
    Server: ngx_openresty
    Date: Mon, 16 Dec 2013 18:03:08 GMT
    Content-Type: text/html; charset=UTF-8
    Content-Length: 0
    Connection: keep-alive
    X-Robots-Tag: noindex
    X-Content-Type-Options: nosniff
    Expires: Wed, 11 Jan 1984 05:00:00 GMT
    Cache-Control: no-cache, must-revalidate, max-age=0
    Pragma: no-cache
    X-Frame-Options: SAMEORIGIN
    ———————————————————-

    thanks!

Viewing 13 replies - 16 through 28 (of 28 total)
  • The topic ‘Visits on the site counted twice’ is closed to new replies.