Not displaying all data

I have a script (below) that lists all of the shoutcast servers that I am using as well as their stats. There is one portion of the script that isn't working right. The part where it is suppose to display the countries from which users are connected, I can't tell what it is doing but it isn't displaying everything. It is either only displaying the first entry for each server or it is taking the first three entries from the first server and splitting them up between the three servers displayed.

Here is the script in action:  http://www.emphaticradio.com/serverstatus.php

The number of countries for each server should match the number of listeners on that server.

Any help would be much appreciated.

//////SCRIPT//////

<?

/* ---------- General configuration ---------- */

/*
  $RCSfile: status.php,v $
  $Revision: 1.34 $


$module_name = basename(dirname(__FILE__));
require("geostats/shoutcast.class.phps");
require("geostats/geoip.phps");


$index = 1;
is_readable("geostats/GeoIP.dat") or exit("Error: GeoIP.dat not found!");

function elapsedtime($seconds) {
      if ($seconds > 86400) {
            $seconds -= 86400;
            return(gmdate("j\d H:i:s", $seconds));
      } else
            return(gmdate("H:i:s", $seconds));
}

$listenersArray["Total"]["current"] = 0;
$listenersArray["Total"]["max"] = 0;


$station_name = "Emphatic Radio.com!";

$refresh = "60";  // Page refresh time in seconds. Put 0 for no refresh
$timeout = "1"; // Number of seconds before connecton times out - a higher value will slow the page down if any servers are offline

/* ----------- Server configuration ---------- */

// Note: dont include http://
// Main server: The song title will be taken from this server

$ip[1] = "xxxx";
$port[1] = "xxxx";
$location[1] = "U.K.";
$password[1] = "xxxx";

$ip[2] = "xxxx";
$port[2] = "xxxx";
$location[2] = "U.K.";
$password[2] = "xxxx";

$ip[3] = "xxxx";
$port[3] = "xxxx";
$location[3] = "U.S.";
$password[3] = "xxxx";
/* ------------------------------------------- */
$servers = count($ip);
?>

<?

$i = "1";

while($i<=$servers)
      {
      $fp = @fsockopen($ip[$i],$port[$i],$errno,$errstr,$timeout);
      if (!$fp)
            {
            $listeners[$i] = "0";
            $msg[$i] = "<span class=\"red\">ERROR [Connection refused / Server down]</span>";
            $error[$i] = "1";
            }
      else
            {
            fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla\r\n\r\n");
            while (!feof($fp))
                  {
                  $info = fgets($fp);
                  }
            $info = str_replace('<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>', "", $info);
            $info = str_replace('</body></html>', "", $info);
            $stats = explode(',', $info);
            if (empty($stats[1]) )
                  {
                  $listeners[$i] = "0";
                  $msg[$i] = "<span class=\"red\">ERROR [There is no source connected]</span>";
                  $error[$i] = "1";
                  }
            else
                  {
                  if ($stats[1] == "1")
                        {
                        $song[$i] = $stats[6];
                        $listeners[$i] = $stats[0];
                        $max[$i] =  $stats[3];
                        $bitrate[$i] = $stats[5];
                        $peak[$i] = $stats[2];
                  
                              $msg[$i] = "<span class=\"red\">Server is up at $bitrate[$i] kbps with $listeners[$i] of $max[$i] listeners</span><p><b>Listener peak:</b> $peak[$i]";
                  }
                        
                  else
                        {
                        $listeners[$i] = "0";
                        $msg[$i] = "    <span class=\"red\">ERROR [Cannot get info from server]</span>";
                        $error[$i] = "1";
                        }
                  }
            }
            
      $i++;
      }
$total_listeners = array_sum($listeners) ;
$total_slots = array_sum($max) ;

$availableslots = ($total_slots-$total_listeners);

print "<div id=\"blu\">\n  <div style=\"text-align: center;\">\n    <font size=\"4\">\nThere are $total_listeners listeners locked <br> $availableslots available slots remaining


</font>\n  </div>\n</div>\n<div>\n  <div>\n    <p><b>Current song:</b> $song[1]</p>\n  </div>\n</div>\n<div>\n";
$i = "1";
while($i<=$servers)
      {
          print "  <div>\n";
if ($max[$i] > 0)
      {
      $percentage = round(($listeners[$i] / $max[$i] * 100));
      $timesby = (300 / $max[$i]);
      $barlength = round(($listeners[$i] * "$timesby"));
      }
if ($error[$i] = 1)
      {
?>
<br>
    <table width="400"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25%" align="center"><b>Server <? print $i ?> (<? print $location[$i] ?>)</b>&nbsp;&nbsp;</td>
        <td width="75%" colspan="3" bgcolor="#eeeeee"><img src="images/<? if ($percentage == "100") { print "red-"; } ?>bar.gif" width="<? print $barlength ?>" height="12" alt="The server is at <? print $percentage; ?>% capacity"></td>
      </tr>
      <tr>
        <td width="25%">&nbsp;</td>
        <td width="25%">0%</td>
        <td width="25%" align="center">50%</td>
        <td width="25%" align="right">100%</td>
      </tr>
    </table>
<?
      }
else
      {
?>
<br>
    <table width="400"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25%" align="center"><b>Server <? print $i ?> (<? print $location[$i] ?>)</b>&nbsp;&nbsp;</td>
        <td width="75%" colspan="3" bgcolor="#eeeeee">&nbsp;</td>
      </tr>
      <tr>
        <td width="25%">&nbsp;</td>
        <td width="25%">0%</td>
        <td width="25%" align="center">50%</td>
        <td width="25%" align="right">100%</td>
      </tr>
    </table>
<?

}

      $fp = @fsockopen($ip[$i],$port[$i],$errno,$errstr,$timeout);
      if (!$fp)
            {
            $listeners[$i] = "0";
            $msg[$i] = "<span class=\"red\">ERROR [Connection refused / Server down]</span>";
            $error[$i] = "1";
            }
      else
            {
            fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla\r\n\r\n");
            while (!feof($fp))
                  {
                  $info = fgets($fp);
                  }
            $info = str_replace('<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>', "", $info);
            $info = str_replace('</body></html>', "", $info);
            $stats = explode(',', $info);
            if (empty($stats[1]) )
                  {
                  $listeners[$i] = "0";
                  $msg[$i] = "<span class=\"red\">ERROR [There is no source connected]</span>";
                  $error[$i] = "1";
                  }
            else
                  {
                  if ($stats[1] == "1")
                        {
                        $song[$i] = $stats[6];
                        $listeners[$i] = $stats[0];
                        $max[$i] =  $stats[3];
                        $bitrate[$i] = $stats[5];
                        $peak[$i] = $stats[2];
      



$gi = geoip_open("geostats/GeoIP.dat", GEOIP_STANDARD);
for ($count = 0;$count < count($servers); $count++) {
      $shoutcast = new ShoutCast();
      $shoutcast->name = $location[$i];
      $shoutcast->host = $ip[$i];
      $shoutcast->port = $port[$i];
      $shoutcast->passwd = $password[$i];

      $listenersArray["$shoutcast->name"]["current"] = "NA";
      $listenersArray["$shoutcast->name"]["max"] = "NA";


      if ($shoutcast->openstats()) {
            // We got the XML, gogogo!..
            if ($shoutcast->GetStreamStatus()) {
                  $listenersArray["$shoutcast->name"]["current"] = $shoutcast->GetCurrentListenersCount();
                  $listenersArray["$shoutcast->name"]["max"] = $shoutcast->GetMaxListenersCount();
                  $listenersArray["Total"]["current"] += $shoutcast->GetCurrentListenersCount();
                  $listenersArray["Total"]["max"] += $shoutcast->GetMaxListenersCount();


                  $listeners_geo = $shoutcast->GetListeners();
                        if (is_array($listeners_geo)) {
                        for ($is=0;$is<sizeof($listeners_geo);$is++) {
                              $country_id = geoip_country_id_by_name($gi, $listeners_geo[$is]["hostname"]);
                              $country_name = $GLOBALS['GEOIP_COUNTRY_NAMES'][$country_id];
                              if (!$country_id) {
                                    $country_id = 0;
                                    $country_name = "Earth(unidentified)";
                              }
                              $from["$country_id"]++;
                        }
                  } else {
                        echo "                                          
                                                Noone listens right now..
                                          \n";
                  }
                  echo "                                    \n";
            } else {
                  echo "Server is up, but no stream available..
                                          
                                    \n";
            }
      } else {
            // Ohhh, damnit..
            echo $shoutcast->geterror();
            echo "\n                                          \n                                    \n";
      }
      echo "                                    \n";

$count++;
}
geoip_close($gi);
?>
<br>
<table border=\"1\" id=\"table1\" cellspacing=\"0\" cellpadding=\"0\" align="center">
        <tr>
                <td>Flag</td>
                <td>Country</td>
                <td>Listeners</td>
        </tr>
        <tr>
<?php
arsort($from);
reset($from);
while (list($key, $val) = each($from)) {
      $country_code = $GLOBALS['GEOIP_COUNTRY_CODES']["$key"];
      $country_name = $GLOBALS['GEOIP_COUNTRY_NAMES']["$key"];
      if (!$country_code) {
            if (!$key && $key != 0) {
                  $country_code = $key;
            } else {
                  // I don't want to show unidentified hosts in ranking.
                  // $country_code = "N/A";
                  continue;
            }
      }
      echo"<td><img src=\"geostats/$country_code.GIF\" alt=\"flag of the $country_name\" /></td><td>$country_name</td>
                        <td>      $val</td></tr></table>
                        \n";
      $countrycount++;
      if ($countrycount >= $countrylimit) break;

}



            
                              $msg[$i] = "<span class=\"red\">Server is up at $bitrate[$i] kbps with $listeners[$i] of $max[$i] listeners</span>
<br>
<br>                                          
                                                 <b>Web Hits:</b> ".$shoutcast->GetWebHitsCount()."<br /> <b>Stream Hits:</b> ".$shoutcast->GetStreamHitsCount()."<br /> <b>Average Listen Time:</b> ".elapsedtime($shoutcast->GetAverageListenTime())."

<p><b>Listener Peak:</b> $peak[$i]";


                  }
                        
                  else
                        {
                        $listeners[$i] = "0";
                        $msg[$i] = "    <span class=\"red\">ERROR [Cannot get info from server]</span>";
                        $error[$i] = "1";
                        }
                  }
            }

print "    <p><b>Status:</b> $msg[$i]</p>\n  </div>\n  <div class=\"line\"> </div>\n";
      $i++;
      }
print "</div>\n";
$time_difference = "0"; // BST: 1 GMT: 0
$time_difference = ($time_difference * 60 * 60);
$time = date("h:ia", time() + $time_difference);
$date = date("jS F, Y", time() + 0);
print "<div>\n  <div>\n    <p><b>Live SHOUTcast statistics:</b> $date, $time</p>\n  </div>\n</div>\n";
?>
<div>
  <div>
MDauphinais1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

steelseth12Commented:
How many servers should there be ???? From what i can see in the code you posted you define 3 servers

$ip[1] = "xxxx";
$port[1] = "xxxx";
$location[1] = "U.K.";
$password[1] = "xxxx";

$ip[2] = "xxxx";
$port[2] = "xxxx";
$location[2] = "U.K.";
$password[2] = "xxxx";

$ip[3] = "xxxx";
$port[3] = "xxxx";
$location[3] = "U.S.";
$password[3] = "xxxx";

and in the url you gave it deisplays 3 servers.
0
MDauphinais1Author Commented:
There should be 3.

The server stats (the bar that displays the percentage of users), number of users is correct.

The part that is problematic is the table under the percentage bar where it shows the country flag, country name and number of listeners from that country for each server. It doesn't add up. It should show countries for every user connected to that server but it doesn't. It is either only displaying the first entry for each server or it is taking the first three entries from the first server and splitting them up between the three servers displayed. I'm not sure which but it doesn't add up.
0
steelseth12Commented:
i think your problem might be here .....

if ($countrycount >= $countrylimit) break;

i cant find anywhre that $countrylimit is define so it must beak after the first relut is printed ...

either remove that line or set $countrylimit to the number of records you want to show
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

MDauphinais1Author Commented:
OK, changing that it is definately starting to look better. But if you check out that page again the listener count under the countries doesn't equal where is says Status: there are x number of listeners...
0
MDauphinais1Author Commented:
It almost looks like it is taking the countries from Server 1 and then adding them to Server 2 so Server 2 is actually displaying the countries from Server 1 and 2. And then Server 3 is showing the countries from all 3 instead of just the ones from Server 3.
0
steelseth12Commented:
try like this ....

<?

/* ---------- General configuration ---------- */

/*
  $RCSfile: status.php,v $
  $Revision: 1.34 $


$module_name = basename(dirname(__FILE__));
require("geostats/shoutcast.class.phps");
require("geostats/geoip.phps");


$index = 1;
is_readable("geostats/GeoIP.dat") or exit("Error: GeoIP.dat not found!");

function elapsedtime($seconds) {
      if ($seconds > 86400) {
            $seconds -= 86400;
            return(gmdate("j\d H:i:s", $seconds));
      } else
            return(gmdate("H:i:s", $seconds));
}

$listenersArray["Total"]["current"] = 0;
$listenersArray["Total"]["max"] = 0;


$station_name = "Emphatic Radio.com!";

$refresh = "60";  // Page refresh time in seconds. Put 0 for no refresh
$timeout = "1"; // Number of seconds before connecton times out - a higher value will slow the page down if any servers are offline

/* ----------- Server configuration ---------- */

// Note: dont include http://
// Main server: The song title will be taken from this server

$ip[1] = "xxxx";
$port[1] = "xxxx";
$location[1] = "U.K.";
$password[1] = "xxxx";

$ip[2] = "xxxx";
$port[2] = "xxxx";
$location[2] = "U.K.";
$password[2] = "xxxx";

$ip[3] = "xxxx";
$port[3] = "xxxx";
$location[3] = "U.S.";
$password[3] = "xxxx";
/* ------------------------------------------- */
$servers = count($ip);
?>

<?

$i = "1";

while($i<=$servers)
      {
      $fp = @fsockopen($ip[$i],$port[$i],$errno,$errstr,$timeout);
      if (!$fp)
            {
            $listeners[$i] = "0";
            $msg[$i] = "<span class=\"red\">ERROR [Connection refused / Server down]</span>";
            $error[$i] = "1";
            }
      else
            {
            fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla\r\n\r\n");
            while (!feof($fp))
                  {
                  $info = fgets($fp);
                  }
            $info = str_replace('<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>', "", $info);
            $info = str_replace('</body></html>', "", $info);
            $stats = explode(',', $info);
            if (empty($stats[1]) )
                  {
                  $listeners[$i] = "0";
                  $msg[$i] = "<span class=\"red\">ERROR [There is no source connected]</span>";
                  $error[$i] = "1";
                  }
            else
                  {
                  if ($stats[1] == "1")
                        {
                        $song[$i] = $stats[6];
                        $listeners[$i] = $stats[0];
                        $max[$i] =  $stats[3];
                        $bitrate[$i] = $stats[5];
                        $peak[$i] = $stats[2];
                 
                              $msg[$i] = "<span class=\"red\">Server is up at $bitrate[$i] kbps with $listeners[$i] of $max[$i] listeners</span><p><b>Listener peak:</b> $peak[$i]";
                  }
                       
                  else
                        {
                        $listeners[$i] = "0";
                        $msg[$i] = "    <span class=\"red\">ERROR [Cannot get info from server]</span>";
                        $error[$i] = "1";
                        }
                  }
            }
           
      $i++;
      }
$total_listeners = array_sum($listeners) ;
$total_slots = array_sum($max) ;

$availableslots = ($total_slots-$total_listeners);

print "<div id=\"blu\">\n  <div style=\"text-align: center;\">\n    <font size=\"4\">\nThere are $total_listeners listeners locked <br> $availableslots available slots remaining


</font>\n  </div>\n</div>\n<div>\n  <div>\n    <p><b>Current song:</b> $song[1]</p>\n  </div>\n</div>\n<div>\n";
$i = "1";
while($i<=$servers)
      {
          print "  <div>\n";
if ($max[$i] > 0)
      {
      $percentage = round(($listeners[$i] / $max[$i] * 100));
      $timesby = (300 / $max[$i]);
      $barlength = round(($listeners[$i] * "$timesby"));
      }
if ($error[$i] = 1)
      {
?>
<br>
    <table width="400"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25%" align="center"><b>Server <? print $i ?> (<? print $location[$i] ?>)</b>&nbsp;&nbsp;</td>
        <td width="75%" colspan="3" bgcolor="#eeeeee"><img src="images/<? if ($percentage == "100") { print "red-"; } ?>bar.gif" width="<? print $barlength ?>" height="12" alt="The server is at <? print $percentage; ?>% capacity"></td>
      </tr>
      <tr>
        <td width="25%">&nbsp;</td>
        <td width="25%">0%</td>
        <td width="25%" align="center">50%</td>
        <td width="25%" align="right">100%</td>
      </tr>
    </table>
<?
      }
else
      {
?>
<br>
    <table width="400"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25%" align="center"><b>Server <? print $i ?> (<? print $location[$i] ?>)</b>&nbsp;&nbsp;</td>
        <td width="75%" colspan="3" bgcolor="#eeeeee">&nbsp;</td>
      </tr>
      <tr>
        <td width="25%">&nbsp;</td>
        <td width="25%">0%</td>
        <td width="25%" align="center">50%</td>
        <td width="25%" align="right">100%</td>
      </tr>
    </table>
<?

}

      $fp = @fsockopen($ip[$i],$port[$i],$errno,$errstr,$timeout);
      if (!$fp)
            {
            $listeners[$i] = "0";
            $msg[$i] = "<span class=\"red\">ERROR [Connection refused / Server down]</span>";
            $error[$i] = "1";
            }
      else
            {
            fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla\r\n\r\n");
            while (!feof($fp))
                  {
                  $info = fgets($fp);
                  }
            $info = str_replace('<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>', "", $info);
            $info = str_replace('</body></html>', "", $info);
            $stats = explode(',', $info);
            if (empty($stats[1]) )
                  {
                  $listeners[$i] = "0";
                  $msg[$i] = "<span class=\"red\">ERROR [There is no source connected]</span>";
                  $error[$i] = "1";
                  }
            else
                  {
                  if ($stats[1] == "1")
                        {
                        $song[$i] = $stats[6];
                        $listeners[$i] = $stats[0];
                        $max[$i] =  $stats[3];
                        $bitrate[$i] = $stats[5];
                        $peak[$i] = $stats[2];
     



$gi = geoip_open("geostats/GeoIP.dat", GEOIP_STANDARD);
for ($count = 0;$count < count($servers); $count++) {
      $shoutcast = new ShoutCast();
      $shoutcast->name = $location[$i];
      $shoutcast->host = $ip[$i];
      $shoutcast->port = $port[$i];
      $shoutcast->passwd = $password[$i];

      $listenersArray["$shoutcast->name"]["current"] = "NA";
      $listenersArray["$shoutcast->name"]["max"] = "NA";


      if ($shoutcast->openstats()) {
            // We got the XML, gogogo!..
            if ($shoutcast->GetStreamStatus()) {
                  $listenersArray["$shoutcast->name"]["current"] = $shoutcast->GetCurrentListenersCount();
                  $listenersArray["$shoutcast->name"]["max"] = $shoutcast->GetMaxListenersCount();
                  $listenersArray["Total"]["current"] += $shoutcast->GetCurrentListenersCount();
                  $listenersArray["Total"]["max"] += $shoutcast->GetMaxListenersCount();


                  $listeners_geo = $shoutcast->GetListeners();
                        if (is_array($listeners_geo)) {
                        for ($is=0;$is<sizeof($listeners_geo);$is++) {
                              $country_id = geoip_country_id_by_name($gi, $listeners_geo[$is]["hostname"]);
                              $country_name = $GLOBALS['GEOIP_COUNTRY_NAMES'][$country_id];
                              if (!$country_id) {
                                    $country_id = 0;
                                    $country_name = "Earth(unidentified)";
                              }
                              $from[$i]["$country_id"]++;
                        }
                  } else {
                        echo "                                         
                                                Noone listens right now..
                                          \n";
                  }
                  echo "                                    \n";
            } else {
                  echo "Server is up, but no stream available..
                                         
                                    \n";
            }
      } else {
            // Ohhh, damnit..
            echo $shoutcast->geterror();
            echo "\n                                          \n                                    \n";
      }
      echo "                                    \n";

$count++;
}
geoip_close($gi);
?>
<br>
<table border=\"1\" id=\"table1\" cellspacing=\"0\" cellpadding=\"0\" align="center">
        <tr>
                <td>Flag</td>
                <td>Country</td>
                <td>Listeners</td>
        </tr>
        <tr>
<?php
arsort($from[$i]);
reset($from[$i]);
while (list($key, $val) = each($from[$i])) {
      $country_code = $GLOBALS['GEOIP_COUNTRY_CODES']["$key"];
      $country_name = $GLOBALS['GEOIP_COUNTRY_NAMES']["$key"];
      if (!$country_code) {
            if (!$key && $key != 0) {
                  $country_code = $key;
            } else {
                  // I don't want to show unidentified hosts in ranking.
                  // $country_code = "N/A";
                  continue;
            }
      }
      echo"<td><img src=\"geostats/$country_code.GIF\" alt=\"flag of the $country_name\" /></td><td>$country_name</td>
                        <td>      $val</td></tr></table>
                        \n";
      $countrycount++;
      if ($countrycount >= $countrylimit) break;

}



           
                              $msg[$i] = "<span class=\"red\">Server is up at $bitrate[$i] kbps with $listeners[$i] of $max[$i] listeners</span>
<br>
<br>                                          
                                                 <b>Web Hits:</b> ".$shoutcast->GetWebHitsCount()."<br /> <b>Stream Hits:</b> ".$shoutcast->GetStreamHitsCount()."<br /> <b>Average Listen Time:</b> ".elapsedtime($shoutcast->GetAverageListenTime())."

<p><b>Listener Peak:</b> $peak[$i]";


                  }
                       
                  else
                        {
                        $listeners[$i] = "0";
                        $msg[$i] = "    <span class=\"red\">ERROR [Cannot get info from server]</span>";
                        $error[$i] = "1";
                        }
                  }
            }

print "    <p><b>Status:</b> $msg[$i]</p>\n  </div>\n  <div class=\"line\"> </div>\n";
      $i++;
      }
print "</div>\n";
$time_difference = "0"; // BST: 1 GMT: 0
$time_difference = ($time_difference * 60 * 60);
$time = date("h:ia", time() + $time_difference);
$date = date("jS F, Y", time() + 0);
print "<div>\n  <div>\n    <p><b>Live SHOUTcast statistics:</b> $date, $time</p>\n  </div>\n</div>\n";
?>
<div>
  <div>
0
MDauphinais1Author Commented:
The page doesn't even load now. It just goes completely blank.
0
steelseth12Commented:
on second thought ... just add       unset($from); after  // We got the XML, gogogo!..

but i cant test either of these as i dont have the data.
0
steelseth12Commented:
<?

/* ---------- General configuration ---------- */

/*
  $RCSfile: status.php,v $
  $Revision: 1.34 $


$module_name = basename(dirname(__FILE__));
require("geostats/shoutcast.class.phps");
require("geostats/geoip.phps");


$index = 1;
is_readable("geostats/GeoIP.dat") or exit("Error: GeoIP.dat not found!");

function elapsedtime($seconds) {
      if ($seconds > 86400) {
            $seconds -= 86400;
            return(gmdate("j\d H:i:s", $seconds));
      } else
            return(gmdate("H:i:s", $seconds));
}

$listenersArray["Total"]["current"] = 0;
$listenersArray["Total"]["max"] = 0;


$station_name = "Emphatic Radio.com!";

$refresh = "60";  // Page refresh time in seconds. Put 0 for no refresh
$timeout = "1"; // Number of seconds before connecton times out - a higher value will slow the page down if any servers are offline

/* ----------- Server configuration ---------- */

// Note: dont include http://
// Main server: The song title will be taken from this server

$ip[1] = "xxxx";
$port[1] = "xxxx";
$location[1] = "U.K.";
$password[1] = "xxxx";

$ip[2] = "xxxx";
$port[2] = "xxxx";
$location[2] = "U.K.";
$password[2] = "xxxx";

$ip[3] = "xxxx";
$port[3] = "xxxx";
$location[3] = "U.S.";
$password[3] = "xxxx";
/* ------------------------------------------- */
$servers = count($ip);
?>

<?

$i = "1";

while($i<=$servers)
      {
      $fp = @fsockopen($ip[$i],$port[$i],$errno,$errstr,$timeout);
      if (!$fp)
            {
            $listeners[$i] = "0";
            $msg[$i] = "<span class=\"red\">ERROR [Connection refused / Server down]</span>";
            $error[$i] = "1";
            }
      else
            {
            fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla\r\n\r\n");
            while (!feof($fp))
                  {
                  $info = fgets($fp);
                  }
            $info = str_replace('<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>', "", $info);
            $info = str_replace('</body></html>', "", $info);
            $stats = explode(',', $info);
            if (empty($stats[1]) )
                  {
                  $listeners[$i] = "0";
                  $msg[$i] = "<span class=\"red\">ERROR [There is no source connected]</span>";
                  $error[$i] = "1";
                  }
            else
                  {
                  if ($stats[1] == "1")
                        {
                        $song[$i] = $stats[6];
                        $listeners[$i] = $stats[0];
                        $max[$i] =  $stats[3];
                        $bitrate[$i] = $stats[5];
                        $peak[$i] = $stats[2];
                 
                              $msg[$i] = "<span class=\"red\">Server is up at $bitrate[$i] kbps with $listeners[$i] of $max[$i] listeners</span><p><b>Listener peak:</b> $peak[$i]";
                  }
                       
                  else
                        {
                        $listeners[$i] = "0";
                        $msg[$i] = "    <span class=\"red\">ERROR [Cannot get info from server]</span>";
                        $error[$i] = "1";
                        }
                  }
            }
           
      $i++;
      }
$total_listeners = array_sum($listeners) ;
$total_slots = array_sum($max) ;

$availableslots = ($total_slots-$total_listeners);

print "<div id=\"blu\">\n  <div style=\"text-align: center;\">\n    <font size=\"4\">\nThere are $total_listeners listeners locked <br> $availableslots available slots remaining


</font>\n  </div>\n</div>\n<div>\n  <div>\n    <p><b>Current song:</b> $song[1]</p>\n  </div>\n</div>\n<div>\n";
$i = "1";
while($i<=$servers)
      {
          print "  <div>\n";
if ($max[$i] > 0)
      {
      $percentage = round(($listeners[$i] / $max[$i] * 100));
      $timesby = (300 / $max[$i]);
      $barlength = round(($listeners[$i] * "$timesby"));
      }
if ($error[$i] = 1)
      {
?>
<br>
    <table width="400"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25%" align="center"><b>Server <? print $i ?> (<? print $location[$i] ?>)</b>&nbsp;&nbsp;</td>
        <td width="75%" colspan="3" bgcolor="#eeeeee"><img src="images/<? if ($percentage == "100") { print "red-"; } ?>bar.gif" width="<? print $barlength ?>" height="12" alt="The server is at <? print $percentage; ?>% capacity"></td>
      </tr>
      <tr>
        <td width="25%">&nbsp;</td>
        <td width="25%">0%</td>
        <td width="25%" align="center">50%</td>
        <td width="25%" align="right">100%</td>
      </tr>
    </table>
<?
      }
else
      {
?>
<br>
    <table width="400"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="25%" align="center"><b>Server <? print $i ?> (<? print $location[$i] ?>)</b>&nbsp;&nbsp;</td>
        <td width="75%" colspan="3" bgcolor="#eeeeee">&nbsp;</td>
      </tr>
      <tr>
        <td width="25%">&nbsp;</td>
        <td width="25%">0%</td>
        <td width="25%" align="center">50%</td>
        <td width="25%" align="right">100%</td>
      </tr>
    </table>
<?

}

      $fp = @fsockopen($ip[$i],$port[$i],$errno,$errstr,$timeout);
      if (!$fp)
            {
            $listeners[$i] = "0";
            $msg[$i] = "<span class=\"red\">ERROR [Connection refused / Server down]</span>";
            $error[$i] = "1";
            }
      else
            {
            fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla\r\n\r\n");
            while (!feof($fp))
                  {
                  $info = fgets($fp);
                  }
            $info = str_replace('<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>', "", $info);
            $info = str_replace('</body></html>', "", $info);
            $stats = explode(',', $info);
            if (empty($stats[1]) )
                  {
                  $listeners[$i] = "0";
                  $msg[$i] = "<span class=\"red\">ERROR [There is no source connected]</span>";
                  $error[$i] = "1";
                  }
            else
                  {
                  if ($stats[1] == "1")
                        {
                        $song[$i] = $stats[6];
                        $listeners[$i] = $stats[0];
                        $max[$i] =  $stats[3];
                        $bitrate[$i] = $stats[5];
                        $peak[$i] = $stats[2];
     



$gi = geoip_open("geostats/GeoIP.dat", GEOIP_STANDARD);
for ($count = 0;$count < count($servers); $count++) {
      $shoutcast = new ShoutCast();
      $shoutcast->name = $location[$i];
      $shoutcast->host = $ip[$i];
      $shoutcast->port = $port[$i];
      $shoutcast->passwd = $password[$i];

      $listenersArray["$shoutcast->name"]["current"] = "NA";
      $listenersArray["$shoutcast->name"]["max"] = "NA";


      if ($shoutcast->openstats()) {
            // We got the XML, gogogo!..
                  
                  unset($from);
            if ($shoutcast->GetStreamStatus()) {
                  $listenersArray["$shoutcast->name"]["current"] = $shoutcast->GetCurrentListenersCount();
                  $listenersArray["$shoutcast->name"]["max"] = $shoutcast->GetMaxListenersCount();
                  $listenersArray["Total"]["current"] += $shoutcast->GetCurrentListenersCount();
                  $listenersArray["Total"]["max"] += $shoutcast->GetMaxListenersCount();


                  $listeners_geo = $shoutcast->GetListeners();
                        if (is_array($listeners_geo)) {
                        for ($is=0;$is<sizeof($listeners_geo);$is++) {
                              $country_id = geoip_country_id_by_name($gi, $listeners_geo[$is]["hostname"]);
                              $country_name = $GLOBALS['GEOIP_COUNTRY_NAMES'][$country_id];
                              if (!$country_id) {
                                    $country_id = 0;
                                    $country_name = "Earth(unidentified)";
                              }
                              $from["$country_id"]++;
                        }
                  } else {
                        echo "                                         
                                                Noone listens right now..
                                          \n";
                  }
                  echo "                                    \n";
            } else {
                  echo "Server is up, but no stream available..
                                         
                                    \n";
            }
      } else {
            // Ohhh, damnit..
            echo $shoutcast->geterror();
            echo "\n                                          \n                                    \n";
      }
      echo "                                    \n";

$count++;
}
geoip_close($gi);
?>
<br>
<table border=\"1\" id=\"table1\" cellspacing=\"0\" cellpadding=\"0\" align="center">
        <tr>
                <td>Flag</td>
                <td>Country</td>
                <td>Listeners</td>
        </tr>
        <tr>
<?php
arsort($from);
reset($from);
while (list($key, $val) = each($from)) {
      $country_code = $GLOBALS['GEOIP_COUNTRY_CODES']["$key"];
      $country_name = $GLOBALS['GEOIP_COUNTRY_NAMES']["$key"];
      if (!$country_code) {
            if (!$key && $key != 0) {
                  $country_code = $key;
            } else {
                  // I don't want to show unidentified hosts in ranking.
                  // $country_code = "N/A";
                  continue;
            }
      }
      echo"<td><img src=\"geostats/$country_code.GIF\" alt=\"flag of the $country_name\" /></td><td>$country_name</td>
                        <td>      $val</td></tr></table>
                        \n";
      $countrycount++;
      if ($countrycount >= $countrylimit) break;

}



           
                              $msg[$i] = "<span class=\"red\">Server is up at $bitrate[$i] kbps with $listeners[$i] of $max[$i] listeners</span>
<br>
<br>                                          
                                                 <b>Web Hits:</b> ".$shoutcast->GetWebHitsCount()."<br /> <b>Stream Hits:</b> ".$shoutcast->GetStreamHitsCount()."<br /> <b>Average Listen Time:</b> ".elapsedtime($shoutcast->GetAverageListenTime())."

<p><b>Listener Peak:</b> $peak[$i]";


                  }
                       
                  else
                        {
                        $listeners[$i] = "0";
                        $msg[$i] = "    <span class=\"red\">ERROR [Cannot get info from server]</span>";
                        $error[$i] = "1";
                        }
                  }
            }

print "    <p><b>Status:</b> $msg[$i]</p>\n  </div>\n  <div class=\"line\"> </div>\n";
      $i++;
      }
print "</div>\n";
$time_difference = "0"; // BST: 1 GMT: 0
$time_difference = ($time_difference * 60 * 60);
$time = date("h:ia", time() + $time_difference);
$date = date("jS F, Y", time() + 0);
print "<div>\n  <div>\n    <p><b>Live SHOUTcast statistics:</b> $date, $time</p>\n  </div>\n</div>\n";
?>
<div>
  <div>
0
MDauphinais1Author Commented:
unset($from);  fixed the problem.

Thanks for your help!
0
steelseth12Commented:
Glad i could help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.