Capture visitor IP on a Wordpress site in.txt file

Asked

Viewed 359 times

2

I am trying to register the IP, Referrer and date of connection of all visitors to my Wordpress site in a.txt file, but I am not being successful.

I tried with the Plugin 'Webrtc IP Grabber & Logger (STUN Vpns)', but this only records the IP of the server itself where the site is located.

I have already searched many sites and forums the same issue, but also unsuccessful, because I have no idea how to add.php scripts to my pages in order to achieve this effect, and it also seems to me that there are no other plugins (free) with the same functionality.

So my question though seems 'duplicate' is...

How do I log all this information into a.txt file?

OR..

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

  • 1

    Don’t you have an access log on the server? On Nginx servers these are usually on /var/log/nginx/access.log

2 answers

1

Copying and pasting codes found in the OS and WPSE, follows 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 is shot on all pages of frontend being visited.

The text file is generated/stored inside 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);
});

-2

In phpMyadmin look for the table that has the ips. then look for the export button. it will export the complete table in a file that you~e can put on me before downloading type : ip.sql, so just rename the ip.sql file to ip.txt. i use the sublime text to view the file and delete what I won’t need.

this link explains how you do it: https://serverpilot.io/docs/how-to-export-a-database-using-phpmyadmin/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.