0

I have a script that should whitelist people on my minecraft server remotely from my webserver.

This script works perfectly when ran from my localhost, but not when ran on my web server (Connection timeout). Is there anything that would make this happen?

<?php

    require('../private/config.php');
    function connectDB($user, $pass, $db) {
        try {   
            return(new PDO("mysql:host=localhost;dbname=" . $db . ";charset=utf8", $user, $pass));
        } catch(PDOException $ex) {
            return $ex;
        }
    }
    $db = connectDB($dbUser, $dbPass, $dbName);
    if ($db instanceof PDOException) {
        die ($db->getMessage());
    }


    if(!isset($_GET['user']) || $_GET['user']=='') {
        die('User undefined.');
    }
    if(!isset($_GET['pw']) || $_GET['pw']=='') {
        die('Not Authorised.');
    }
    if($_GET['pw']!=$whitelistpassword) {
        die('Not Authorised.');
    }



    $user = $_GET['user'];

    define( 'MQ_SERVER_ADDR', '[ip]' );
    define( 'MQ_SERVER_PORT', 25605 );
    define( 'MQ_SERVER_PASS', 'passwordtest' );
    define( 'MQ_TIMEOUT', 5 );

    require 'MinecraftQuery/MinecraftRcon.class.php';

    try
    {
        $Rcon = new MinecraftRcon;

        $Rcon->Connect( MQ_SERVER_ADDR, MQ_SERVER_PORT, MQ_SERVER_PASS, MQ_TIMEOUT );

        $Data = $Rcon->Command("whitelist add ".$user);

        if($Data===false) {
            throw new MinecraftRconException("Failed to get command result.");
        }
        else if(StrLen($Data)==0) {
            throw new MinecraftRconException("Got command result, but it's empty.");
        }

        //echo HTMLSpecialChars($Data);
    }
    catch( MinecraftRconException $e )
    {
        header('Location: approve.php?pw='.$whitelistpassword);
        die('Error');
    }
    $Rcon->Disconnect();

    $sql = "UPDATE `Applications` SET `Approved` = 1 WHERE `Minecraft` = :user";
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':user', $user);
    $stmt->execute();
    header('Location: approve.php?pw='.$whitelistpassword);
?>

Every time I run it from the webserver, it times out. Then the minecraft server crashes.

Expected: "User" has been added to whitelist (works from localhost) Actual from web server:

Warning: fsockopen() [function.fsockopen]: unable to connect to [ip]:25605 (Connection timed out) in /home/dooog/public_html/MinecraftQuery/MinecraftRcon.class.php on line 38

Warning: Cannot modify header information - headers already sent by (output started at /home/dooog/public_html/MinecraftQuery/MinecraftRcon.class.php:38) in /home/dooog/public_html/whitelistsend.php on line 57
S..
  • 5,511
  • 2
  • 36
  • 43
Davis Mariotti
  • 574
  • 1
  • 4
  • 23
  • 2
    *"It doesn't work"* [doesn't explain the problem](http://stuck.include-once.org/#help3) enough. You need to elaborate on your input, expected and actual outcomes, or concretise error messages. And if you didn't write said script or don't know programming or that language, we won't be able to help you. SO is not a debugging service. – mario Feb 18 '13 at 22:12
  • never include "real" details about logins. it's acceptable to anonymous/mask them. now everyone here has had access to the ip/user/pass for your server. – Marc B Feb 18 '13 at 22:19
  • I took out the ip and added more info, the password is temporary – Davis Mariotti Feb 18 '13 at 22:22
  • So that's what "timed out" means ! ... Well, the script throws an error in another file, not in this one, who knows what happens there? As was said, this is website about programming, so if you just posted this code and don't understand it at all, noone here can help you. If - and only if - you do know a bit of programming, showing the file that actually crashes might help. But my bet is it would still be offtopic :) – enrey Feb 18 '13 at 22:28
  • Just a guess, but on shared hosting webservers a wide array of outgoing ports are commonly firewalled; sometimes all. Try from the shell. Else change provider plans. – mario Feb 18 '13 at 22:32
  • I found the problem. My free webhost was blocking the outgoing port. I guess I need to buy some hosting – Davis Mariotti Feb 18 '13 at 22:34

1 Answers1

0

As far as I'm aware, when trying to connect to a remote database, you must first allow the remote machine to connect. This is done by adding the remote machine to the privileges list in PHPmyAdmin, that's assuming that you are running a new-ish version of mySQL.

Once you have done that then you should be able to communicate between webserver and the MC database.

Xylum
  • 28
  • 5
  • I had figured out through the help of a friend that my free web host was blocking all outgoing ports. thanks for the help though. – Davis Mariotti Feb 19 '13 at 03:31