Check script

Status
Niet open voor verdere reacties.
Ben nog even wat verder gegaan...

Ik kwam erachter dat die nginx server toch wat hardnekkiger is dan ik dacht.
Wanneer je een site opvraagt die achter een nginx server zit ziet hij die ook als offline.

Ik heb nu een wat groter script geschreven die de content van een niet bestaande site (Dus de nginx standaard pagina) vergelijkt met de opgegeven pagina. Wanneer die verschillend zijn wordt alsnog een online bericht gegeven.

Ik kan zelf niet testen wat er achter een nginx server gebeurd. Heb zelf geen nginx meer draaien i.v.m mijn andere sites. Hierbij dus de code. Graag hoor ik van je of de output nu wel goed is.
PHP:
<?php
class checkweb{

	public $config_badurl = "http://thismustbeanonexisting.url";

	// Possible returns, You can edit them by your own strings
	public $errorNoRedirect = "A url redirect is called but no redirect page is given";
	public $errorMultiLoop = "Server is looping to multiple servers.";
	public $errorOfflineNgninx = "Server is offline but nginx is gives me a custom paga.";
	public $onlineMsg = "Server is running and online";
	public $offlineMsg = "Server is offline or blocking me.";
	public $badUrlMsg = "The give url is not a valid url.";
	public $noCurlSupport = "Your php does not support cURL function info: http://www.php.net/manual/en/book.curl.php";

	// Gets a by nginx gennerated page
	private function get_nonexistsContent(){
		$ch = curl_init($this->config_badurl);
        $ret = curl_setopt($ch, CURLOPT_HEADER, true);
        $ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $ret = curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $ret = curl_exec($ch);
        if(empty($ret)){
            $data = curl_error($ch);
            curl_close($ch);
            return $data;
        }
        else{
            $info = curl_getinfo($ch);
            curl_close($ch);
            return $ret;
        }
    }
	
	// Gets the content of the target webpage (For matching it with the nginx page)
	private function get_targetContent($url){
		$ch = curl_init($url);
        $ret = curl_setopt($ch, CURLOPT_HEADER, true);
        $ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $ret = curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $ret = curl_exec($ch);
        if(empty($ret)){
            $data = curl_error($ch);
            curl_close($ch);
            return $data;
        }
        else{
            $info = curl_getinfo($ch);
            curl_close($ch);
            return $ret;
        }
    }

	// Gets the http header information
	private function get_http($headers){
		$http_status_full = $headers[0];
		$status_arr = explode(' ', $http_status_full, 3);
		$http_status = (object) array(
			'version' => $status_arr[0],
			'code' => (int) $status_arr[1],
			'txt' => $status_arr[2]
		);
		return $http_status;
	}

	// Checks if the page is online
	private function progress_check($url){
		$server_header = get_headers($url, 1);
		$http_status = $this->get_http($server_header);
		$http_server = $server_header['Server'];
		if($http_status->code == 301){
			return $this->errorNoRedirect;
		}
		else if($http_status->code == 200){
			// A online page has been found now check if it's a server or empty plesk nginx 
			if($http_server == 'nginx'){
				$bad_page = $this->get_nonexistsContent();
				$target_page = $this->get_targetContent($url);
				if($target_page == $bad_page){
					return $this->errorOfflineNgninx;
				}
				else{
					return $this->onlineMsg;
				}
			}
			else{
				return $this->onlineMsg;
			}
		}
		else{
			return $this->offlineMsg;
		}
	}

	// Start caller - Checks id cURL exists - Validate the given url - Gets the redirect url if a http 301 is called
	public function check_url($url){
		if(!function_exists('curl_init')){
			return $this->noCurlSupport;
		}
		else if(!filter_var($url, FILTER_VALIDATE_URL)){
			return $this->badUrlMsg;
		}
		else{
			$server_header = get_headers($url, 1);
			$http_status = $this->get_http($server_header);
			if($http_status->code == 301){
				$url2 = $server_header['Location'];
				if(!empty($url2)){
					return $this->progress_check($url2);
				}
				else{
					return $this->errorNoRedirect;
				}
			}
			else{
				return $this->progress_check($url);
			}
		}
	}
}
 

// Controleer voor aanvraag
$posturl = 'Geen';
if(isset($_GET['url']) and !empty($_GET['url'])){
    $posturl = urldecode($_GET['url']);
}

$check = new checkweb();
?>
 
<html>
    <head>
        <title>Check URL</title>
    </head>
    <body>
        <table>
            <thead>
                <tr>
                    <th>Opgevraagde URL</th>
                    <th>Status URL</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><?php echo $posturl; ?></td>
                    <td><?php if($posturl != 'Geen'){ echo $check->check_url($posturl); } ?></td>
                </tr>
            </tbody>
        </table>
        <hr/>
        <div id="form">
            <ul>
                <li>
                    <a href="?url=<?php echo urlencode('http://google.nl'); ?>"> http://google.nl </a>
                </li>
                <li>
                    <a href="?url=<?php echo urlencode('http://ikbestaniet.nl'); ?>"> http://ikbestaniet.nl </a>
                </li>
                <li>
                    <a href="?url=<?php echo urlencode('http://helpmij.nl'); ?>"> http://helpmij.nl </a>
                </li>
                <li>
                    <a href="?url=<?php echo urlencode('http://qwsderfgtredf.nl'); ?>"> http://qwsderfgtredf.nl </a>
                </li>
            </ul>
        </div>
    </body>
</html>
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan