Pastebin

New pastes are no longer accepted · Stats

Latest Pastes

Desmond's Server Checker - With Cache

<?php

// ### EDIT BELOW ########################

define( 'CACHE_DIR', 'cache/' ); // Add trailing slash.
define( 'RECHECK_TIME', 5 ); // Time to recheck (In minutes).

// ### STOP EDITING ######################

if ( !isset( $_GET['host'] ) && !isset( $_GET['post'] ) )
	exit; // Stop the script.

$host = $_GET['host'];
$port = $_GET['port'];
	
// Function to check the server status.
function CheckStatus( $host, $port )
{
	// Open the socket on the host and port requested.
	if ( @fsockopen($site, $port, $errno, $errstr, 5 ) )
		file_put_contents( CACHE_DIR . md5($host . $port) . '.cache', 'online,' . time() );
	else
		file_put_contents( CACHE_DIR . md5($host . $port) . '.cache', 'offline,' . time() );
}

// Check to see if there is a cache file for this server.
if ( file_exists( CACHE_DIR . md5($host . $port) . '.cache' ) )
{
	// Open the cache.
	$cache = file_get_contents( CACHE_DIR . md5($host . $port) . '.cache' );
	$cache_a = explode( ',', $cache );
	
	// Check to see if we need to check the server status.
	if ( time() > $cache_a[1] + ( RECHECK_TIME * 60 ) )
		CheckStatus( $host, $port );
}
else
	CheckStatus( $host, $port );

// Open the cache.
$cache = file_get_contents( CACHE_DIR . md5($host . $port) . '.cache' );
$cache_a = explode( ',', $cache );

// Is the server online or offline?
if ( $cache_a[0] == 'online' )
{
	echo 'Server is online!';
}
else
{
	echo 'Server is offline!';
}
?>