Experiment calling Perl access analysis program from WordPress

update Last updated: March 17, 2015 at 12:52 PM

In previous experiments, header.php of the theme in WordPress, simply call perl access analysis program "perlstats.cgi (tentative name)", it has been found that the environment variables of the server will not be carried over.

This time, I experimented with calling perl access analysis program in wordpress index.php program.
In addition, normally, because the environment variable does not seem to pass still, in advance, get the environment variable from the program side of PHP, perl program was also corrected in the method of passing by parameters.

The program code for index.php is as follows:
(For security, the actual code is slightly modified)

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/your_wp_directory/wp-blog-header.php' );
?>

<?php
/**
 * perlstats.cgi
 */
 $pl_host  = getenv('REMOTE_HOST');
 $pl_addr  = getenv('REMOTE_ADDR');
 $pl_agent = getenv('HTTP_USER_AGENT');
 $pl_ref   = getenv('HTTP_REFERER');
 echo file_get_contents("https://senris.com/cgi-bin/perlstats/perlstats.cgi?host=$pl_host&addr=$pl_addr&agent=$pl_agent&ref=$pl_ref");
?>


The following is the screen of the management program that displays the result after executing perlstats.pl.

Looking at the screen above, you can see that the following server environment variables have been passed.

REMOTE_HOST
REMOTE_ADDR
HTTP_USER_AGENT

Unfortunately, the aggregate of the source URL remains '0', so HTTP_REFERER seems not to have been sent.

So, in the first place, to see if the HTTP_REFERER is set correctly in PHP and perl, I made a simple experimental site and verified it.

First, the PHP program "server_key.php" that displays available environment variables. (Below)

<?php
/*
 サーバー環境変数一覧表示(PHP)
 */
 echo "使用可能なサーバー変数一覧(PHP)<br /><br />\n";

 foreach($_SERVER as $server_key => $server_val){
  echo $server_key."="."$server_val<br />\n";
 }
?>


Next is the perl program "server_key.pl" that displays the available environment variables. (Below)

#!/usr/bin/perl
# サーバー環境変数一覧表示(perl)
 
print "Content-Type: text/html\n\n";

print "<html><br />\n";
print " <head><title>サーバー環境変数一覧(perl)</title></head><br />\n";
print "  <body><br />\n";

print "環境変数一覧(perl)<br /><br />\n";

foreach (sort keys %ENV) 
{
  print "$_ = $ENV{$_}<br />\n";
}

print "  </body><br />\n";
print "</html><br />\n";


And the HTML code "test_referer.html" on the side of calling those programs. (Below)

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>HTTP_REFERERのテスト</title>
</head>
<body>
<h1>HTTP_REFERERのテスト<br>
<br>
<a href="https://www.senris.com/test/server_key.pl" target="_blank">ここをクリック(server_key.pl)</a><br>
</a><br>
<a href="https://www.senris.com/test/server_key.php" target="_blank">ここをクリック(server_key.php)</a><br>
</a><br>
<a href="https://www.senris.com" target="_blank">ここをクリック(senris.com)</a><br>
</h1>
</body>
</html>


The result of each of the above two links is as follows:

With this, it was understood that acquisition of the server environment variables was completed properly for both PHP and perl.
If it is not on WordPress, it seems to work without problems.

The problem is the third link (index.php).
In the end, HTTP_REFERER didn't get it.

So, in that there may be a problem with the insertion position of the code into index.php, I changed it as follows.

<?php
/**
 * perlstats.cgi
 */
 $pl_host  = getenv('REMOTE_HOST');
 $pl_addr  = getenv('REMOTE_ADDR');
 $pl_agent = getenv('HTTP_USER_AGENT');
 $pl_ref   = getenv('HTTP_REFERER');
 echo file_get_contents("https://senris.com/cgi-bin/perlstats/perlstats.cgi?host=$pl_host&addr=$pl_addr&agent=$pl_agent&ref=$pl_ref");

/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/your_wp_directory/wp-blog-header.php' );
?>


The result was a spectacular failure. (>_<)

The same result was obtained when placed in the same scope of PHP.
I feel like the HTTP_REFERER information is being reset asynchronously in WordPress plugins such as NewStatPress, but I don't know the exact cause.
Possibly, caching by plug-in "WP Super Cache" may be the cause.

If you know this,Please tell me from ero teacher! !(Lol)

That's...

Since it is difficult to pass the HTTP_REFERER from WordPress to perl programs, perl access analysis program "perlstats.cgi" in the "aggregation of the source URL" part of the give up by saying that it is not possible. Orz

Add this entry to the hasebookmark
X (post)

Leave a Reply