-1

I have got that class below to check servers (get info if server is online, players number etc) in loop. There is about 250+ servers to check and I get gateway timeout while executing scripts.

There is search query for all servers

SearchQuery = $mysqli -> query ( 'SELECT * FROM `list_ots` ORDER BY `list_ots`.`id`' );

while ($Row = $SearchQuery -> fetch_assoc())
    {
        $check_ban=$mysqli->query('SELECT count(*) FROM `list_bans` where `server`="'.$Row['id'].'"')->fetch_assoc();
        if($check_ban['count(*)']!=0){
            continue;
        }

        checkServer($Row);
    }

And thats the class OTSChecker and function getData() with one is executed for every server. The problem is - this script time executing is too long Is there possibility to make it faster(getData function)? I added something like

ini_set('max_execution_time', 600); 

but its not working

<?php
class OTSChecker
{

public $ConnData;
private $OTData=array(), $XML=array(), $Uptime = array(), $TimeOut;
public function __construct($IP, $Port=7171)
{
    $this -> ConnData = array('IP' => $IP, 'Port' => $Port);
}

public function SocketTimeOut( $inTimeOut )
{
    $this -> TimeOut = intval ( $inTimeOut );   
}

public function GetData()
{
    $info = chr(6) . chr(0) . chr(255) . chr(255) . 'info';
    // fsocketopen cannot be with ssl becouse then it gives 0 
    $Sock = @fsockopen( $this->ConnData['IP'] , $this->ConnData['Port'] , $errno, $errstr, 10);

    if ( is_resource ( $Sock ) )
    {
        if ( isset ( $this -> TimeOut ) )
        {
            stream_set_timeout( $Sock , $this -> TimeOut );
            stream_set_blocking( $Sock , FALSE );
        }

        @fwrite( $Sock , $info );

        $Data = NuLL;

        while ( !feof ( $Sock ) )
        {
            $Data .= fgets( $Sock , 1024 );
        }

        @fclose( $Sock );

        

        if ( $Data != NuLL )
        {
            $this->XML = simplexml_load_string ( $Data );
        }

        $this -> OTData['status'] = 'Online';
    } else {
        $this -> OTData['status'] = 'Offline';      
    }
}

public function Status ()
{
    return $this -> OTData['status'];
}

private function GenerateUptime ( &$Data )
{
    preg_match('/uptime="(\d+)"/', $Data, $matches);
    $h = floor($matches[1] / 3600);
    $m = floor(($matches[1] - $h*3600) / 60);
    
    return array ( 'hours' => $h , 'minutes' => $m );
}

public function GetOwnerName()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> owner -> attributes() -> name;
}

public function GetOwnerEmail()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> owner -> attributes() -> email;
}

public function GetServerName()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> serverinfo -> attributes() -> servername;
}

public function GetServerLocation()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> serverinfo -> attributes() -> location;
}

public function GetServerVersion()
{
    if ( is_object ( $this -> XML) )
        //return (string) $this -> XML -> serverinfo -> attributes() -> version;
        return (string) $this -> XML -> serverinfo -> attributes() -> version;
}

public function GetNowUptime()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> serverinfo -> attributes() -> uptime;
}

public function GetCountOfPlayersOnline()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> players -> attributes() -> online;
}

public function GetMaxPlayersCount()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> players -> attributes() -> max;
}

public function GetMaxPlayersRecord()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> players -> attributes() -> peak;
}

public function GetAllMonsters()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> monsters -> attributes() -> total;
}

public function GetMotd()
{
    if ( is_object ( $this -> XML) )
        return (string) $this -> XML -> motd;
}
}
?> 
Josh Coehn
  • 11
  • 2
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Apr 18 '21 at 12:18

1 Answers1

0

"gateway timeout" indicates that a proxy server or load balancer times out (e.g. nginx).

Increasing the script's timeout limit is not enough. You also need to configure the timeouts of the other components leading up to the script execution accordingly.

obe
  • 7,378
  • 5
  • 31
  • 40