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:

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:

and here is the php code:

<?php
/*
Plugin Name: Server Status
Plugin URI: 
Description: Server Status Plugin   
Author: Bradly spicer
Version: 1
Author URI: 
*/

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

Dani AI

Generated

Short answer: the plugin is showing "offline" because the code is trying to treat a Minecraft server like an HTTP server. correctly flagged the protocol/timeout issue, and ’s HostGator test confirms an important hosting constraint — many shared hosts block outgoing raw sockets (fsockopen/socket_create). Using cURL with CURLOPT_PORT only changes the port used for an HTTP request; it does not speak the Minecraft binary ping/query protocols, so you should not expect an HTTP 200 even when the game server is online.

Practical paths forward:

  • Use a purpose-built Minecraft status library (the PHP-Minecraft-Query approach mentioned). That implements the Query or server-list-ping protocols and returns player count/MOTD correctly — but it requires outgoing socket access and, for the Query protocol, that the target server has query enabled.
  • Implement the modern server-list-ping (the client status handshake) instead of HTTP; it works without Query but still needs raw TCP permitted by the host.
  • If your shared host blocks sockets, have the plugin call an external HTTP API (a public Minecraft-status service) or run a tiny pinger on a VPS you control and expose a simple HTTP endpoint. That lets you use cURL on restrictive hosts reliably.
  • Scraping public listing sites is a last resort (fragile and possibly against terms of service).

Troubleshooting checklist: check phpinfo() for disabled functions (fsockopen, socket_create, stream_socket_client), run a simple outgoing-socket test from the server, and confirm with HostGator whether outgoing TCP/UDP to arbitrary ports is allowed. Cache status responses to avoid frequent pings and rate limits. Given the constraints in this thread, the most robust solution for end users on restrictive hosting is an external HTTP-based pinger or requiring a host that permits sockets and using a dedicated Minecraft query implementation.

Recommended Answers

All 11 Replies

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.

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";
?>

What protocol is using?

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.