Hey guys,

I recently started work on Minepress again, I'm having some problems however. I am trying to make a plugin which detects the ip of a minecraft server and results in printing out if its online or not

here is display on my site:

http://img545.imageshack.us/img545/4404/beaa1f33dce74c6dbee14e6.png

as you can see it says offline, however if you go to

http://minecraftservers.org/server/5402

it says online (at this moment in time).

here is the widget options:

http://img62.imageshack.us/img62/901/078ad579040741ff8a0f9a4.png

and here is the php code:

<?php
/*
Plugin Name: Server Status
Plugin URI: http://minepress.co.uk
Description: Server Status Plugin   
Author: Bradly spicer
Version: 1
Author URI: http://minepress.co.uk
*/

include "function.php";

class ServerStatus extends WP_Widget
{
  function ServerStatus()
  {
    $widget_ops = array('classname' => 'ServerStatus', 'description' => 'Displays Server Status' );
    $this->WP_Widget('ServerStatus', 'Server Status display', $widget_ops);
  }

  function form($instance)
  {
    $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
    $title = $instance['title'];
    $ip= $instance['ip'];
    $port= $instance['port'];
?>
  <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
    <p><label for="<?php echo $this->get_field_id('Port'); ?>">Ip: <input class="widefat" id="<?php echo $this->get_field_id('ip'); ?>" name="<?php echo $this->get_field_name('ip'); ?>" type="text" value="<?php echo attribute_escape($ip); ?>" /></label></p>
        <p><label for="<?php echo $this->get_field_id('port'); ?>">Port: <input class="widefat" id="<?php echo $this->get_field_id('port'); ?>" name="<?php echo $this->get_field_name('port'); ?>" type="text" value="<?php echo attribute_escape($port); ?>" /></label></p>
<?php
  }

  function update($new_instance, $old_instance)
  {
    $instance = $old_instance;
    $instance['title'] = $new_instance['title'];
    $instance['ip'] = $new_instance['ip'];
    $instance['port'] = $new_instance['port'];
    return $instance;
  }

  function widget($args, $instance)
  {
    extract($args, EXTR_SKIP);

    echo $before_widget;
    $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
    $ip = empty($instance['ip']) ? ' ' : apply_filters('ip', $instance['ip']);
    $Port = empty($instance['Port']) ? ' ' : apply_filters('Port', $instance['Port']);

    if (!empty($title))
      echo $before_title . $title . $after_title;;

    // WIDGET CODE GOES HERE
?><div id="Server_status"><?php $online = @fsockopen("sublimitygaming.com", 25565, $errno, $errstr, 1);
if($online >= 1) { 
echo 'Online!'; 
}
else {
echo 'Offline!'; 
} 
?>
</div>

 <?php
    echo $after_widget;
  }

}
add_action( 'widgets_init', create_function('', 'return register_widget("ServerStatus");') );?>

I have sliced and diced this code quite a bit so chances are it is probably wrong in a LOT of areas, that and I haven't touched this snippet in a few months

Perhaps the 1 second timeout is too short. Can you check if errno/errstr return values?

Hey Pritaeas, I just contacted Hostgator and they had a restriction on fsocketopen. So I was suggested to try Curl (Something I've not explored).

This is something I'm working on as of this minute which still displays the server as offline:

<?php
function Servercheck($ip,$port){
       $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
       $ch=curl_init();
       curl_setopt ($ch, CURLOPT_URL,$ip );
       curl_setopt($ch, CURLOPT_USERAGENT, $agent);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch,CURLOPT_VERBOSE,false);
       curl_setopt($ch, CURLOPT_TIMEOUT, 5);
       curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
       curl_setopt($ch,CURLOPT_SSLVERSION,3);
       curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
       $page=curl_exec($ch);
       $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
       curl_close($ch);
}
if (Servercheck("play.magicacraft.net", "25567"))
       echo "Minecraft server is up";
else
       echo "Minecraft server is down";
?>

You forgot CURLOPT_PORT. Without it port 80 will be checked. Your function does not return a boolean version yet. Something like:

return $httpcode == 200;

Additional question, does it really use the HTTP protocol?

Thanks again pritaeas for your response.

http://minepress.co.uk/test/index2.php

it says the Minecraft server is down... Which it isn't (Weird). part of me is thinking it isn't putting in the ":" by default for a port?

E.g

play.magicacraft.net:25567

here is the code just incase I made a mistake :P

<?php
function Servercheck($ip, $port){
       $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
       $ch=curl_init();
       curl_setopt ($ch, CURLOPT_URL,$ip );
       curl_setopt($ch, CURLOPT_USERAGENT, $agent);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch,CURLOPT_VERBOSE,false);
       curl_setopt($ch, CURLOPT_TIMEOUT, 5);
       curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
       curl_setopt($ch,CURLOPT_SSLVERSION,3);
       curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
       curl_setopt($ch,CURLOPT_PORT, $port);
       $page=curl_exec($ch);
       $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
       curl_close($ch);
       return $httpcode == 200;
}
if (Servercheck("play.magicacraft.net", "25567"))
       echo "Minecraft server is up";
else
       echo "Minecraft server is down";
?>

I'm not quite sure. The idea is to create a wordpress plugin which detects if Minecraft servers are online or offline.

http://minecraftservers.org/
This is where I grab examples to try. I've seen other plugins which use fsocketopen but as thats blocked this was my last attempt.

Sorry if that doesn't help :P

Tried this as well:

if (Servercheck("72.20.55.85".":", "25570"))

But I came to the conclusion if I am using

curl_setopt($ch,CURLOPT_PORT, $port);

it doesn't matter...

Ok Contacting my host again (Host Gator) I think I dont have socket support which is stupid... I don't know if I'm using outgoing or incoming though

The problem is that it is not using any of the curl recognized protocols. The only info I get is lookup time. All other fields are empty or zero. You could try socket_create but my guess is that it is blocked as well by HostGator.

So it's a pretty dead idea for anyone with Hostgator?

It appears so. In the meantime tried your first code snippet, but that returns a timeout.

You can open the minecraftservers.org/server/5402 page with curl and extract the information from there.

commented: Helpful :) +0

That would mean people who wanted to use my plugin would have more agro than needed :( I't was meant to be easy for people who don't have much web experience to just chuck their information on and roll...

Thanks for your help anyway. up-voted

edit: Just tested original code from up top. It works on Localhost. Damn Hostgator

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.