0

所以我一直在尝试为 wordpress 编写这个小部件,以检查我在本地托管的 minecraft 服务器是否在线。我正在为我的网站使用托管服务,我的代码只是有一点问题,我不知道如何解决它。

<?php


define('PLUGIN_URL', plugin_dir_url( __FILE__ ));

add_action("widgets_init", array('sc_status', 'register'));
register_activation_hook( __FILE__, array('sc_status', 'activate'));
register_deactivation_hook( __FILE__, array('sc_status', 'deactivate'));
class sc_status {
function activate(){
        $scdata = array( 'option1' => 'Default value' ,'option2' => 55);
        if ( ! get_option('sc_status')){
            add_option('sc_status' , $scdata);
        } else {
              update_option('sc_status' , $scdata);
        }
}
function deactivate(){
        delete_option('sc_status');
}
function control(){
        $scdata = get_option('sc_status');
    ?>
        <p><label>Title : <input name="widget_title" type="text" value="<?php echo $scdata['widget_title']; ?>" /></label></p>
        <p><label>IP : <input name="server_ip" type="text" value="<?php echo $scdata['server_ip']; ?>" /></label></p>
        <p><label>Port (25565) : <input name="server_port" size="5"  type="text" value="<?php echo $scdata['server_port']; ?>" /></label></p>
    <?php
        if (isset($_POST['server_ip'])){
        $scdata['widget_title'] = attribute_escape($_POST['widget_title']);
        $scdata['server_ip'] = attribute_escape($_POST['server_ip']);
        $scdata['server_port'] = attribute_escape($_POST['server_port']);
        update_option('sc_status', $scdata);
    }
}
function widget($args){



          wp_enqueue_style('sc', PLUGIN_URL.'/css/sc_button.css');

          $scdata = get_option('sc_status');
          $title = $scdata['widget_title'];
          $port = $scdata['server_port'];
          if(empty($port)) {
              $port = "25565";
          }

        echo $args['before_widget'];
        if(!empty($title)) {
            echo $args['before_title'] . $title . $args['after_title'];
        }
        // Check Server Status
        $waitTimeoutInSeconds = 1; 
        if($fp = fsockopen($scdata['server_ip'],$port,$errCode,$errStr,$waitTimeoutInSeconds)){   
           // It worked 
           echo '<div class="btn btn-success" rel="tooltip" title="first tooltip">online</div>';
        } else {
           // It didn't work 
           echo '<div class="btn btn-danger">offline</div>';
        } 
        fclose($fp);



        // End
        echo $args['after_widget'];
  }
  function register(){
        register_sidebar_widget('Servercraft Status', array('sc_status', 'widget'));
        register_widget_control('Servercraft Status', array('sc_status', 'control'));
  }
}

这适用于“大多数”部分。它的问题是错误是:

警告:fsockopen() [function.fsockopen]: 第 62 行无法连接到(不想发布 ip)(连接超时)

然后它像它应该的那样发布“离线” div .. 其次是

警告:fclose() 期望参数 1 是资源,第 69 行给出的布尔值

所以希望有人可以告诉我我做错了什么或如何解决它。

编辑!这是我对上述部分进行了几次修复后的代码。我仍然遇到的问题是服务器仍在向站点反馈它处于脱机状态......它根本不是。

<?php

define('PLUGIN_URL', plugin_dir_url( __FILE__ ));

add_action("widgets_init", array('sc_status', 'register'));
register_activation_hook( __FILE__, array('sc_status', 'activate'));
register_deactivation_hook( __FILE__, array('sc_status', 'deactivate'));
class sc_status {
function activate(){
        $scdata = array( 'option1' => 'Default value' ,'option2' => 55);
        if ( ! get_option('sc_status')){
            add_option('sc_status' , $scdata);
        } else {
              update_option('sc_status' , $scdata);
        }
}
function deactivate(){
        delete_option('sc_status');
}
function control(){
        $scdata = get_option('sc_status');
    ?>
        <p><label>Title : <input name="widget_title" type="text" value="<?php echo $scdata['widget_title']; ?>" /></label></p>
        <p><label>IP : <input name="server_ip" type="text" value="<?php echo $scdata['server_ip']; ?>" /></label></p>
        <p><label>Port (25565) : <input name="server_port" size="5"  type="text" value="<?php echo $scdata['server_port']; ?>" /></label></p>
    <?php
        if (isset($_POST['server_ip'])){
        $scdata['widget_title'] = attribute_escape($_POST['widget_title']);
        $scdata['server_ip'] = attribute_escape($_POST['server_ip']);
        $scdata['server_port'] = attribute_escape($_POST['server_port']);
        update_option('sc_status', $scdata);
    }
}
function widget($args){



          wp_enqueue_style('sc', PLUGIN_URL.'/css/sc_button.css');

          $scdata = get_option('sc_status');
          $title = $scdata['widget_title'];
          $port = $scdata['server_port'];
          if(empty($port)) {
              $port = "25565";
          }

        echo $args['before_widget'];
        if(!empty($title)) {
            echo $args['before_title'] . $title . $args['after_title'];
        }


        // Check Server Status
        $waitTimeoutInSeconds = 1;
        $fp = @fsockopen($scdata['server_ip'],$port,$errCode,$errStr,$waitTimeoutInSeconds);
        echo "<span class='sc_status'> IP : ".$scdata['server_ip']."</span>";
        if($fp){                
            // It worked 
            echo '<div class="btn btn-success" rel="tooltip" title="first tooltip">online</div>';
            fclose($fp);
        } else {
            // It didn't work 
            echo '<div class="btn btn-danger">offline</div>';
        } 




        // End
        echo $args['after_widget'];
  }
  function register(){
        register_sidebar_widget('Servercraft Status', array('sc_status', 'widget'));
        register_widget_control('Servercraft Status', array('sc_status', 'control'));
  }
}

?>

4

1 回答 1

0

非常简单的解决方案:

if($fp = @fsockopen($scdata['server_ip'],$port,$errCode,$errStr,$waitTimeoutInSeconds)){   
 //it worked
 fclose($fp);
}          

您只需使用 @ 隐藏错误消息。并且只有在连接打开时才关闭连接(所以转到fclose($fp);“它工作”部分)。

于 2013-01-30T23:38:45.180 回答