2

I have a question about a PHP script that I wrote to connect to a collect statistics from IRC servers, as a test as I am new to PHP and still learning.

Here is the script:

<?php
set_time_limit(0);

$servers = array(
           "irc.icq.com",
           "irc.quakenet.org"
);

function get_statistics ($server, $port) {
    $nick   = 'IRCDir' . rand(1000, 9999);
    $irc = fsockopen($server, $port);

    fputs($irc,"USER $nick 0 * :$nick\n");
    fputs($irc,"NICK $nick\n");


    while ($data = fgets($irc, 128)) {

        $ex = explode(' ', $data);

        if (isset($ex[0]) && $ex[0] == "PING") {
            fputs($irc, "PONG ".$ex[1]."\n");
        }

        if (count($ex) > 0) {

            if (isset($ex[1]) && $ex[1] == "001") {
                $network = $ex[6];
                echo date('h:i:s') . " network: " . $server;
                echo "\n";
            }

            if (isset($ex[1]) && $ex[1] == "002") {
                $server = str_replace(',', '', $ex[6]);
                echo date('h:i:s') . " server: " . $server;
                echo "\n";
            }

            if (isset($ex[1]) && $ex[1] == "251") {
                $users   = $ex[5] + $ex[8];
                $servers = $ex[11];
                echo date('h:i:s') . " users: " . $users;
                echo "\n";
                echo date('h:i:s') . " servers: " . $servers;
                echo "\n";
            }

            if (isset($ex[1]) && $ex[1] == "252") {
                $ircops = $ex[3];
                echo date('h:i:s') . " ircops: " . $ircops;
                echo "\n";
            }

            if (isset($ex[1]) && $ex[1] == "254") {
                $channels = $ex[3];
                echo date('h:i:s') . " channels: " . $channels;
                echo "\n";
            }

        }

    }
    fclose($irc);
}

foreach ($servers as $server) {
    echo date('h:i:s') . " getting statistics for " . $server;
    echo "\n";
    get_statistics($server, '6667');
}

exit;
?>

Here is the output:

root@li140-48:~# php bot.php
11:04:23 getting statistics for irc.freenode.net
11:04:24 network: irc.freenode.net
11:04:24 server: sendak.freenode.net
11:04:24 users: 90601
11:04:24 servers: 26
11:04:24 ircops: 22
11:04:24 channels: 50958
11:05:25 getting statistics for irc.icq.com
11:05:26 network: irc.icq.com
11:05:26 server: irc-k01a.orange.icq.com
11:05:26 users: 2671
11:05:26 servers: 3
11:05:26 ircops: 16
11:05:26 channels: 810
11:06:26 getting statistics for irc.quakenet.org
11:06:28 network: irc.quakenet.org
11:06:28 server: blacklotus.ca.us.quakenet.org
11:06:28 users: 37648
11:06:28 servers: 40
11:06:28 ircops: 67
11:06:28 channels: 26711
root@li140-48:~#

My question is what causes the delay between the servers being indexed?

At 10.24:19 it has completed the index of irc.icq.com so at this point it should disconnect and immediately index the next server in the array, but instead of that it is waiting exactly a minute before doing so, but I have not mentioned a minute in the script?

Hoping some PHP gurus can lend a hand!

user4428391
  • 381
  • 1
  • 3
  • 8
  • Is it always exactly 60 seconds? – ggdx Jan 07 '15 at 10:58
  • 1
    Considering that it is *exactly* 60 seconds, it looks like PHP is waiting for something, or the IRC server is waiting for something. Either way, something is not letting the connection go, and it has to time out. – Niet the Dark Absol Jan 07 '15 at 10:58
  • @dwhite.me I am adding more servers to the array and updating the question with more results. – user4428391 Jan 07 '15 at 11:06
  • @NiettheDarkAbsol I want the script to close the connection once all of the variables have been collected. I guess I am doing this wrong or have `fclose` in the wrong place ... – user4428391 Jan 07 '15 at 11:12
  • my guess is your pong code is broken and your never making it out of the loop. its always waiting for the server to end the connection. – exussum Jan 07 '15 at 11:23

0 Answers0