2

At the beginning of the PHP that generates all the pages of my site is like

log_visitor_info(
    $_SERVER['REMOTE_ADDR'],
    $_SERVER['REQUEST_URI'],
    $_SERVER['HTTP_REFERER'],
    $_SERVER['HTTP_USER_AGENT']
);

which calls the function

function log_visitor_info ( $ip, $pgurl, $refurl, $aginfo )
{
    global $wpdb, $ipsToIgnore; 
    if (!in_array($ip, $ipsToIgnore)) {
        $wpdb->insert('wp_nas_visits',  array(
            'ip'=>$ip,
            'refurl'=>$refurl,
            'pgurl'=>$pgurl,
            'aginfo'=>$aginfo
        ));
    }
}

where the $wpdb->insert is inserting something to the database. Because I don't need any of this info anywhere else in the page, I would prefer if it were possible to execute log_visitor_info asynchronously or "in the background", if you will. I think it's slowing down my page loads. Is there some way that I can put log_visitor_info on my server's queue that executes separately (if such a thing even exists ...) ? I have Windows Server 2012.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • 2
    Possible duplicate of [php execute a background process](http://stackoverflow.com/questions/45953/php-execute-a-background-process) – showdev Nov 18 '15 at 22:24
  • Can't you just get all of this from the server logs? – Rob G Nov 18 '15 at 22:38
  • If not, look into an Ajax call on page load that will handle the logging – Rob G Nov 18 '15 at 22:39
  • @RobGudgeon Yes, I can get this from server logs, but I have to write a script to parse the info out of them. Plus, this is for a website I want to be able to repackage with a custom analytics feature. –  Nov 18 '15 at 22:42
  • And yes, AJAX is a good idea, but can't I directly access the same vein of processing that an AJAX call would??? –  Nov 18 '15 at 22:43
  • You could put it at the bottom of each page, for a quick win. Call flush() first to reduce blocking. Or you could fire something using exec() and tell it to redirect output so it doesn't cause the script to hang. – Rob G Nov 18 '15 at 22:56
  • A parsing script wouldn't take much effort. I run done that aren't even code, just grep, sed, awk & pipes/redirects – Rob G Nov 18 '15 at 22:57

1 Answers1

2

Not tested and I'm not a WordPress guy or fan, but should be adaptable if you want to use AJAX. First, create a PHP page (logger.php) and add the following:

//include files needed to instantiate $wpdb

$ipsToIgnore  = unserialize($argv[1]);
$dataToInsert = unserialize($argv[2]);

if (!in_array($dataToInsert['ip'], $ipsToIgnore)) $wpdb->insert('wp_nas_visits', $dataToInsert);

Second, in the beginning of the PHP that generates all the pages of your site replace your function and function call with:

$ips  = escapeshellarg(serialize($ipsToIgnore));
$args = escapeshellarg(serialize(array('ip'     => $_SERVER['REMOTE_ADDR'],
                                       'pgurl'  => $_SERVER['REQUEST_URI'],
                                       'refurl' => $_SERVER['HTTP_REFERER'],
                                       'aginfo' => $_SERVER['HTTP_USER_AGENT'])));

pclose(popen("start /B /path/to/php.exe /path/to/logger.php $ips $args", "r"));

You may need to escapeshellarg() the path to php.exe and the path to logger.php as well, especially if they have spaces in them.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • See, but that's not something that can be included in a WordPress theme that can install itself. –  Nov 19 '15 at 00:05