Online or Offline PHP Code Snippet

HoddzDJ

Member
Okay guys and girls, I feel this guide should be here, as I found it useful for my site!! After adding the code snippets to my website, it came to my attention that the information was not correct, meaning I was getting an Online display even though the stream was offline. Why? I was told by iR that it only really works if you have Auto DJ enabled.

So, I set about building my own script to add to my page and display the true information!! I have been coding PHP for a while now, and after finding a few scripts out there already I have built my own for you to use if you wish to!! This simply looks up if the server is streaming (Online) or if it's not (Offline) and gives you a true read out, even if you don't have Auto DJ enabled.

PHP:
<?php
/*
This page was built by Ben Hodder.
http://www.facebook.com/benhodder  //  http://www.pukkaradio.net
It is provided free of charge and there is no warranty with it.
February 2nd, 2010. 11:45 GMT
*/

// Edit the values below to suite your server and site.
$server = "ip.of.your.server"; // IP of the server
$port = "8000"; // Port of the server
$online = "On Air"; // Text to display if the server is online
$offline = "Off Air"; // Text to display if the server is offline
// Do not edit below here unless you know exactly what you're doing.

// Start Data Collection
 $fp = fsockopen("$server", $port, $errno, $errstr, 30);
      fputs($fp, "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)\r\n\r\n");
      while (!feof($fp)) {
          $content .= fgets($fp,128);
}
      fclose($fp);
// start data processing
      $debut = strpos($content, '<body>') + strlen('<body>');
      $fin = strpos($content, '</body>', $debut);
      $string = substr($content, $debut, $fin - $debut);
      $stats = explode(',', $string);


//offline
if ($stats[1] == "0" || !$stats) {
echo("$offline");
}
//online
if ($stats[1] == "1") {
echo("$online");
}
?>
How To
For this to work, you must have a PHP based page. Where you want to display the text, insert the above code making edits for your server. If you wish, you can have that as a basic PHP page and then insert in an iFrame, it's up to you.

Disclaimer
Feel free to distribute this as much as you like, and edit it where you wish. There is no warranty with this, please leave the credit for me in there as it took me a fair few hours to build it.

End User Support
Help for this script will only be given on this thread, do not PM me, I will not reply. Yes, it will work for servers outside of iR, in-fact it will work for any SHOUTcast server.
 
Top