Capture visitor IP on a WordPress site in .txt file

2

I'm trying to register the IP, Referrer, and the link date of all visitors to my Wordpress site in a .txt file, but I'm not being sucessed.

I've tried the Plugin ' WebRTC IP Grabber & Logger (STUN VPNs) ', but this only registers the IP of the server where the site is located.

I have already looked at many sites and forums the same question, but also unsuccessful, because I have no idea how to add scripts.php my pages in order to achieve this effect, and I also do not seem to have other Plugins free) with the same functionality.

So my question though looks like ' duplicate ' is ...

How do I register all this information in a .txt file?

OU ..

How do I extract this information from phpMyAdmin into a .txt file?

    
asked by anonymous 02.04.2017 / 15:52

1 answer

0

By copying and pasting codes found in the OS and WPSE, follow a very simple plugin to do this. I added a function to capture the type of page being viewed (home, category, search, tag, etc). The plugin works on wp_head and fires on every frontend page being visited.

The text file is generated / stored within http://example.com/wp-content/ficheiro.txt . Personally, I think it would be preferable to store this in the database.

<?php
/** 
  * Plugin Name: (B5F) Capturar IP para ficheiro.txt
  * Version: 1.0
  * Author: brasofilo
  */

# https://wordpress.stackexchange.com/a/83896
function wpse8170_loop() {
    global $wp_query;
    $loop = 'notfound';

    if ( $wp_query->is_page ) {
        $loop = is_front_page() ? 'front' : 'page';
    } elseif ( $wp_query->is_home ) {
        $loop = 'home';
    } elseif ( $wp_query->is_single ) {
        $loop = ( $wp_query->is_attachment ) ? 'attachment' : 'single';
    } elseif ( $wp_query->is_category ) {
        $loop = 'category';
    } elseif ( $wp_query->is_tag ) {
        $loop = 'tag';
    } elseif ( $wp_query->is_tax ) {
        $loop = 'tax';
    } elseif ( $wp_query->is_archive ) {
        if ( $wp_query->is_day ) {
            $loop = 'day';
        } elseif ( $wp_query->is_month ) {
            $loop = 'month';
        } elseif ( $wp_query->is_year ) {
            $loop = 'year';
        } elseif ( $wp_query->is_author ) {
            $loop = 'author';
        } else {
            $loop = 'archive';
        }
    } elseif ( $wp_query->is_search ) {
        $loop = 'search';
    } elseif ( $wp_query->is_404 ) {
        $loop = 'notfound';
    }

    return $loop;
}

# http://stackoverflow.com/a/13646735
function getUserIP() {
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP)) {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
        $ip = $forward;
    }
    else {
        $ip = $remote;
    }

    return $ip;
}


add_action('wp_head', function(){
    $ip = getUserIP();
    $current_page = wpse8170_loop();
    $print = $ip . ' | ' . $current_page; // <--- PERSONALIZAR
    $logs = WP_CONTENT_DIR . '/ficheiro.txt';
    # http://stackoverflow.com/a/24972441
    $myfile = file_put_contents($logs, $print . PHP_EOL, FILE_APPEND | LOCK_EX);
});
    
05.05.2017 / 01:15