Please excuse my stupidity, I'm a nub My problem is as follows:
I am creating a webpage that has a bunch of my friends webcams and displays whether they active or not. The webcam software we use basically runs a webserver on our computers and can be accessed by going to http://<ip>:<port>. At first, I simply used the fopen function to determine if the page was active, however when I found that it took so long to work (It works, but takes about 1min-1.5mins to load) I downloaded a more complicated script from the internet that I don't quite understand at this point in my PHP career. Same result, works but takes forever. Any help would be much appreciated.
<tr class="cam"> <?php $name = "Goodman"; $ip3 = "67.87.xxx.xxx"; if (disp_link("http://$ip3:10000", "$name")){ $s = 'yes'; } else { $s = 'no'; } ?>
<td><div align="center">10/17/03</div></td>
<td><div align="center"><?php echo"<a href=\"#\" onclick=\"window.open('cam.php?name=$name&ip3=$ip3&s=$s', '$name', 'height=240,width=320');\">$name</a>";?></div></td>
<td><div align="center"><?php if (disp_link("http://$ip3:10000", "$name")){ echo '<img src="online.gif"'; } else { echo '<img src="offline.gif"';} ?></div></td>
</tr>
Each row is like this
I have also required validate.php at the top of my script. This is the part I don't exactly understand :
<?
ob_start();
$debug = 0;
$timeout = 30; // give up if can't connect within 30 secs.
$check_freq = 5*60; // cache for 5 minutes.
@session_register('statuses');
@session_register('hostnames');
function flush_disp_link_cache() {
global $statuses;
global $hostnames;
$statuses = array();
$hostnames = array();
}
function disp_link($url, $text) {
global $debug;
global $timeout;
global $statuses;
global $hostnames;
global $check_freq;
$now = time();
$e = error_reporting(); error_reporting($e & (255-E_WARNING));
if (!preg_match('/^(http|https|ftp)://((?:[a-zA-Z0-9_-]+.?)+):?(d*)/', $url, $m)) {
// it's a relative link, or absolute link with unsupported protocol
// so there's no need to check
$sstatus = '';
if ($debug) { $sstatus='DEBUG:UNCHECKED'; }
} else {
$proto=$m[1];
$hostname=strtolower($m[2]);
$port=$m[3];
// is the host an IP address?
if (preg_match('/^d+.d+.d+.d+/', $hostname)) {
$ip = $hostname;
} else { // it's not, so we have to resolve it first
// have we tried to resolve it not so long ago?
$from_cache=0;
if (isset($hostnames[$hostname])) {
if ($debug) {
echo "DEBUG: last resolve = ", $hostnames[$hostname][1], "<br>";
}
if ($now - $hostnames[$hostname][1] <= $check_freq) {
$ip = $hostnames[$hostname];
$from_cache=1;
}
}
if (!$from_cache) {
// we haven't, so resolve it
$ip = gethostbyname($hostname);
// if the hostname was not resolvable, gethostbyname returns
// its argument unchanged
if ($ip === $hostname) { $ip=''; }
// cache this resolve
$hostnames[$hostname]=array($ip,$now);
}
}
if (!$ip) { // was the hostname unresolvable?
$sstatus = 'HOST NOT FOUND';
} else {
// get universal port number defaults, if not specified
if (!$port) {
if ($proto == 'http') { $port = 80; }
elseif ($proto == 'https') { $port = 443; }
elseif ($proto == 'ftp') { $port = 21; }
}
$key = "$ip:$port";
// have we checked the site not so long ago?
$from_cache=0;
if (isset($statuses[$key])) {
if ($debug) {
echo "DEBUG: last check = ",$statuses[$key][2], "<br>";
}
if ($now-$statuses[$key][2] <= $check_freq) {
$sstatus = $statuses[$key][0];
$from_cache=1;
}
}
if (!$from_cache || ($from_cache && ($sstatus == "OK"))) {
// we haven't, so check it
// CR: or we have and the host is ok, so check the file
if ($debug) {
echo "DEBUG: checking: proto=$proto, hostname=$hostname, ",
"ip=$ip, port=$port...<br>";
}
$fp = fsockopen($hostname, $port, &$errno, &$errstr, $timeout);
if ($debug) {
echo "DEBUG: connect result: fp=$fp, errno=$errno, errstr=$errstr<br>";
}
if ($fp) {
$sstatus = "OK";
fputs( $fp, sprintf( "GET %s HTTP/1.0\n\n", $url ) );
for( $try = 1; $try <= 3; $try++ )
{
$fstatus = "CHECKING";
if( ($got = fgets( $fp, 256 )) == NULL )
break;
if( eregi( "HTTP/1.(.) (.*) (.*)", $got, $parts ) )
{
echo "<!-- Found on try $try -->";
if( $parts[2] == "200" )
$fstatus = "FOUND";
else if( $parts[2] == "300" )
$fstatus = "MOVED";
else if( $parts[2] == "403" )
$fstatus = "RESTRICTED";
else if( $parts[2] == "404" )
$fstatus = "NOT FOUND";
else
$fstatus = "ERR ".$parts[2]." - ".$parts[3];
break;
}
$fstatus = "Bad Comms";
}
} else {
if (preg_match('/timed?[- ]?out/i', $errstr)) {
$sstatus = "TIMEOUT";
} elseif (preg_match('/refused/i', $errstr)) {
$sstatus = "OFF";
} else {
$sstatus = "DOWN?";
}
}
// cache this check
$statuses[$key] = array($sstatus, $fstatus, $now);
}
}
}
if ($sstatus) { }
/* If the server is up, tell how the file is doing... */
if ($sstatus == "OK") { return true; }
else { return false; }
}
ob_end_flush();
?>
Sorry for asking such a long question! Thanks for your help you guys!