• 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 15 replies - 1 through 15 (of 28 total)
  • Debug what is happening by placing a HTML comment in the code like this.

    function bdetector_insert_useragent()
    {
    	global $wpdb;
    
    	$table_name = $wpdb->prefix . 'bdetector';
    
    	$wpdb->insert($table_name,array('user_agent'=>$_SERVER['REMOTE_ADDR']),array('%s'));
    echo '<!--WordPress Tracker-->';
    }

    Now open your page and check the footer to see home many times this comment has been ‘echo’ed.

    There is a possibility that some plugin is doing AJAX calls or something else which triggers do_action('wp-footer'); (just my guess).

    Compare the plugins and their settings between the localhost installation and your live one.

    Thread Starter mrapi

    (@mrapi)

    Hi Jesin,thanks for your answer.

    echo message is put just one time,after some google search I used a cookie,now seems ok,I’m testing it.

    code:

    function bdetector_insert_useragent()
    { if (!isset($_COOKIE['visited']))
     {
        setcookie('visited',1);
    	global $wpdb;
    
    	$table_name = $wpdb->prefix . 'bdetector';
    
    	$wpdb->insert($table_name,array('user_agent'=>$_SERVER['REMOTE_ADDR']),array('%s'));
    
     }
    }

    With this cookie the entire visit will just be counted as 1.

    So if a visitor browses 3 pages on your site only one row will be inserted in the “bdetector” table.

    Thread Starter mrapi

    (@mrapi)

    ok,no problem it is ok
    the only problem left: CURRENT_TIMESTAMP set time with -6 hours delay as my current timezone,how to set correct time/timezone?
    thanks!

    If your host features custom php.ini file use the following config

    date.timezone=<timezone>

    Or edit your .htaccess file.

    php_value date.timezone [timezone name]

    List of timezone https://php.net/manual/en/timezones.php

    Further reading https://jesin.tk/setting-a-timezone-in-php/

    Thread Starter mrapi

    (@mrapi)

    Hi Jesin
    I can’t edit php.ini because hosting is a private server,I only can access cpanel and my domain folders/files
    in public_html folder I added this line in htaccess:
    php_value date.timezone Europe/Bucharest
    but then my page show this:

    The server encountered an internal error or misconfiguration and was unable to complete your request.
    
    Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error.
    
    More information about this error may be available in the server error log.
    
    Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
    
    Apache Server at ....com Port 80

    thanks!

    Remove the line for .htaccess ,edit the wp-config.php file and place the following in the end

    @ini_set( 'date.timezone', 'Europe/Bucharest' );

    Thread Starter mrapi

    (@mrapi)

    Hi,it is not working ??

    my current time is 13:42 and in database it puts 06:34
    also I found now 2 duplicated records ?? , very strange, the difference is about 1 second between them,how can a cookie expire so fast,I set it to expire when browser closes (expire time not set)
    https://img163.imageshack.us/img163/8044/ii9g.jpg
    thanks!

    Set the correct timezone in General > Settings page and use the current_time() function

    Thread Starter mrapi

    (@mrapi)

    thanks,great,that works:

    $wpdb->insert($table_name,array('user_agent'=>$_SERVER['REMOTE_ADDR'],
    	                                'hit_date'=>current_time( 'mysql' )
    	                                ),
     	                          array('%s','%s')
    	             );

    but duplicate values are back with 1 second between,even I set cookie ??

    What is your site’s URL?

    Thread Starter mrapi

    (@mrapi)

    The site sends a cookie named exps_visited so you should check that in the if condition.

    HTTP/1.1 200 OK
    Server: ngx_openresty
    Date: Thu, 12 Dec 2013 12:33:15 GMT
    Content-Type: text/html; charset=UTF-8
    Content-Length: 12292
    Connection: keep-alive
    Vary: Accept-Encoding
    X-Pingback: https://www.softexpertcta.info/wordpress/xmlrpc.php
    Set-Cookie: exps_visited=1
    Cache-Control: max-age=0
    Expires: Thu, 12 Dec 2013 12:33:14 GMT
    Thread Starter mrapi

    (@mrapi)

    It was renamed,this is the complete code:

    <?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');
    
    function bdetector_insert_useragent()
    { 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')
    	             );
    
       // echo '<!--WordPress Tracker-->';
     }
    }
    
    add_action('wp_footer','bdetector_insert_useragent');

    thanks

    I sent requests to your site thrice using Curl with the User Agent set to “Jesin A”. The second and third time I set the cookie to 1.

    Check your database for the number of entries.

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