【问题标题】:Write from WordPress plugin to text file with PHP使用 PHP 从 WordPress 插件写入文本文件
【发布时间】:2014-10-22 23:19:24
【问题描述】:

我正在尝试从 WordPress 插件将日期写入文本文件。虽然这适用于单个 PHP 文件,但当我将代码添加到插件时它不会写入。 TXT 文件的权限为777,与插件文件在同一目录下。

我做错了什么?

这是插件,我添加的行在 //log 404s to text file 块中:

 <?php
 /*
 Plugin Name: Mail me 404 errors
 Plugin URI: http://me.com
 Description: A 404 status triggers an email with details.
 Version: 1.0
 Author: Me
 Author URI: http://me.com
 */
 //SENDS 404 EMAIL TO ADMIN


 function email_admin($location){   
     // ip address
     $ipaddress = $_SERVER['REMOTE_ADDR'];
     if (!empty($_SERVER['X_FORWARDED_FOR'])) {
         $X_FORWARDED_FOR = explode(',', $_SERVER['X_FORWARDED_FOR']);
     }
     elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
         $X_FORWARDED_FOR = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
     }
     else {$ipaddress = "undefined";}
     if (!empty($X_FORWARDED_FOR)) {
         $ipaddress = trim($X_FORWARDED_FOR[0]);
     }

     // site info
     $blname=get_option('blogname');
     $admemail = get_option('admin_email');
     $honeypot = "http://www.projecthoneypot.org/ip_".$ipaddress;

     // time log
     $time = date("F jS Y, H:i", time()+25200);

     //referrer
     function current_page_url(){
         $page_url = 'http';
         if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'){
         $page_url .= 's';
     }
     return $page_url.'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
 }
 if(isset($_SESSION['referrer'])){
    $referrer = $_SESSION['referrer'];
 } elseif(isset($_SERVER['HTTP_REFERER'])){
    $referrer = $_SERVER['HTTP_REFERER'];
 } else {$referrer = "undefined";}
    $_SESSION['referrer'] = current_page_url();

 // query string
 if (isset($_SERVER['QUERY_STRING'])) { 
    $string = $_SERVER['QUERY_STRING']; 
 } else {
    $string = "undefined";
 }

 // request URI
 if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER["HTTP_HOST"])) {
    $request = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
 } else {
    $request = "undefined";
 }

 // identity
 if (isset($_SERVER['REMOTE_IDENT'])) {
    $remote = $_SERVER['REMOTE_IDENT'];
 } else {
    $remote = "undefined";
 }

 // user agent
 if (isset($_SERVER['HTTP_USER_AGENT'])) {
    $agent = $_SERVER['HTTP_USER_AGENT'];
 } else {
    $agent = "undefined";
 }

 //log 404s to txt file
 $ipad = $_SERVER['REMOTE_ADDR'];
 $ban = "#$time\r\n$ipad\r\n"; 
 $file = "errors.txt"; 
 $open = @fopen($file, "a"); 
 $write = @fputs($open, $ban); 
 @fclose($open);
 //log 404s to txt file

 $mailhead = "MIME-Version: 1.0\r\n";
 $mailhead .= "Content-type: text/plain; charset=UTF-8\r\n";
 $mailhead .= 'From: "' . $blname . '" <' .$admemail. ">\r\n";
 $mailsubj= $blname.': 404 error';
 $mailintro = "Someone wanted to go to ".$request.", but it doesn't exist. Maybe you can have a look and see if anything needs to be fixed.\r\n";
 $mailbody=

    $mailintro . "\n" .

    "TIME: "            . $time    . "\n" . 
    "*404: "            . $request . "\n" . 
    "REFERRER: "        . $referer . "\n" . 
    "QUERY STRING: "    . $string  . "\n" . 
    "REMOTE ADDRESS: "  . $ipaddress . "\n" . 
    "REMOTE IDENTITY: " . $remote  . "\n" . 
    "USER AGENT: "      . $agent   . "\n" .
    "CHECK WHOIS: https://who.is/whois-ip/ip-address/". $ipaddress . "\n" .
    "CHECK IP ADDRESS: " . $honeypot . "\n\n\n";

 @mail($admemail,$mailsubj,$mailbody,$mailhead);
 }
 function mail_me_errors(){
    global $wp_query;
    $location=$_SERVER['REQUEST_URI'];
     if ($wp_query->is_404){
        email_admin($location);
     }
 }
 add_action('get_header', 'mail_me_errors');
 ?>

【问题讨论】:

  • 您正在使用 @ 前缀抑制来自 fopenfputs 调用的错误报告。您可以尝试从这些调用中删除@,看看您的插件会抛出什么错误。
  • 我找不到与此插件相关的任何错误。

标签: php wordpress fopen fwrite


【解决方案1】:

plugin_dir_path()将文件/wp-content/plugins/your-plugin/errors.txt的完整路径传递给fopen

$file = plugin_dir_path( __FILE__ ) . '/errors.txt'; 
$open = fopen( $file, "a" ); 

以下是一个最小示例:

add_action( 'get_header', 'mail_me_errors' );

function mail_me_errors() {
    if ( is_404() ) {
        email_admin( $_SERVER['REQUEST_URI'] );
    }
}

function email_admin( $location ) {   
    $time = date( "F jS Y, H:i", time()+25200 );
    $ban = "#$time\r\n$location\r\n"; 
    $file = plugin_dir_path( __FILE__ ) . '/errors.txt'; 
    $open = fopen( $file, "a" ); 
    $write = fputs( $open, $ban ); 
    fclose( $open );
}

【讨论】:

  • 谢谢。这样就完成了工作。
  • 比搞乱error_log 更容易,这似乎不太灵活
  • @JDandChips 这也可以在生产中使用。
猜你喜欢
  • 2023-04-09
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多