Jump to content

Recommended Posts

Posted

I keep getting frustrated with my server status script which checks my 2 remote servers like IRC and my Game server statuses.

if 1 of those servers are offline the script keeps trying to reach it and site wont load until it reaches the server. So i would want a time out feature to my status checker so it kills the check if it takes too long and then returns offline status.

Here is the script:

 

 <?php

 // Status By NecroNet
 // Config, Edit this!	(You could add this to your config file)

 $IP = "irc.domain.tld";		 //Server IP-address
 $Port = "3306";		 //Port for server (standard is 25565)

 $title =  "IRC-Server";   // Name of Server
 $flash_file  = "statusbar.swf"; // Location of the flash file



 // Don't edit anything under this!

$p = "$title";
	$fp = @fsockopen($IP, $Port, $errno, $errstr);
if (!$fp) {
	$t= "Offline";
$bgcolor = "#FF0000";
	} else {
	$t= "Online";
$bgcolor = "#00FF00";
	}
	@fclose($fp);
 echo "<embed src='".$flash_file."?t=".$p."&m=".$t."' quality='high' bgcolor='".$bgcolor."' width='176' height='60'></embed>";


   ?>

php side checks the server and then loads flash box saying is it online or not.

Posted

Add 30 as the last parameter to this line:

 

$fp = @fsockopen($IP, $Port, $errno, $errstr, 30);

 

That should kill it after 30 seconds if I'm not mistaken.

Posted

Here's a way I'm more familiar with using cURL:

 

<?php
$url = "http://helionet.org/";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);

# here's where you set number of seconds before time out
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);

$content = curl_exec($ch);
$header = curl_getinfo($ch);
$time = $header['connect_time'];
$httpcode = $header['http_code'];
curl_close($ch);

# if http code is good return site is up
if($httpcode >= 200 && $httpcode < 303)
{
echo "Site is up and working!<br>";
}
else
{
echo "Site is down!<br>";
}

# if connection time is 0 return connection timed out
if ($time == "0")
{
echo "Connection Timed Out After 15 Seconds!";
}
?>

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...