English 中文(简体)
网址是:www.un.org。
原标题:php get_headers a good way to tell if a site is up?

我对加拿大语来说仍然很新,你能否就以下法典是否很好发表意见,以说明某一地点是否正在关闭或缩小,如果它不合适为什么和更好的替代因素?

提前感谢。

$siteHeader = @get_headers($url , 1);
if ($siteHeader > 1) {
    $siteUp = true;
} else {
    $siteUp = false;
}
问题回答

i 使用曲线,但我只想:

function check($url, $ignore =   )
{
    $agent = "Mozilla/4.0 (B*U*S)";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);

    curl_setopt($ch, CURLOPT_VERBOSE, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 45);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 45);

    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5); //follow up to 10 redirections - avoids loops


    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //fix for certificate issue
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //fix for certificate issue


    $page = curl_exec($ch);
    $err = curl_error($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    $codes = array(
        0 =>  Domain Not Found ,
        100 =>  Continue ,
        101 =>  Switching Protocols ,
        200 =>  OK ,
        201 =>  Created ,
        202 =>  Accepted ,
        203 =>  Non-Authoritative Information ,
        204 =>  No Content ,
        205 =>  Reset Content ,
        206 =>  Partial Content ,
        300 =>  Multiple Choices ,
        301 =>  Moved Permanently ,
        302 =>  Found ,
        303 =>  See Other ,
        304 =>  Not Modified ,
        305 =>  Use Proxy ,
        307 =>  Temporary Redirect ,
        400 =>  Bad Request ,
        401 =>  Unauthorized ,
        402 =>  Payment Required ,
        403 =>  Forbidden ,
        404 =>  Not Found ,
        405 =>  Method Not Allowed ,
        406 =>  Not Acceptable ,
        407 =>  Proxy Authentication Required ,
        408 =>  Request Timeout ,
        409 =>  Conflict ,
        410 =>  Gone ,
        411 =>  Length Required ,
        412 =>  Precondition Failed ,
        413 =>  Request Entity Too Large ,
        414 =>  Request-URI Too Long ,
        415 =>  Unsupported Media Type ,
        416 =>  Requested Range Not Satisfiable ,
        417 =>  Expectation Failed ,
        500 =>  Internal Server Error ,
        501 =>  Not Implemented ,
        502 =>  Bad Gateway ,
        503 =>  Service Unavailable ,
        504 =>  Gateway Timeout ,
        505 =>  HTTP Version Not Supported 
    );

    $httpcode_out =  http:   . $httpcode .   (  . $codes[$httpcode] .  ) ;
    $err =  curl error:   . $err;

    $out = array(
        $url,
        $httpcode_out,
        $err
    );

    if ($httpcode >= 200 && $httpcode < 307)
    {//good
        return array(
             Work ,
            $out
        );
    }
    else
    {//BAD
        return array(
             Fail ,
            $out
        );
    }
}

视你的情况而定,但特别是如果用户提交URL,我会这样做。

//returns true, if domain is availible, false if not
function isDomainAvailible($domain)
{
   //check if URL is valid
   if(!filter_var($domain, FILTER_VALIDATE_URL)){
           return false;
   }

   $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
   $ch = curl_init();
   curl_setopt ($ch, CURLOPT_URL, $url);
   curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt ($ch,CURLOPT_VERBOSE, FALSE);
   curl_setopt ($ch, CURLOPT_TIMEOUT, 10);
   curl_setopt ($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
   curl_setopt ($ch,CURLOPT_SSLVERSION, 3);
   curl_setopt ($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
   $page=curl_exec($ch);
   //echo curl_error($ch);
   $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
   curl_close($ch);
   if ($httpcode >= 200 && $httpcode < 300) 
    return true;
   else 
    return false;
}

这主要是因为,如果某些服务器没有寄出使用剂,就赢得了一定的答复。

Update: The more I think of it, the less fine it looks. I ve expanded on my initial answer, which in retrospect was naive.

It s fine for basic usage, but you might want to check the HTTP response code as well instead of just checking if you got a response. The way the code is right now, it just tells you that there was someone listening on the other side, which is a long way from what most people would consider "the site is up".

在这里,如果请求失败,如何易于将《吉大港山区行动计划》的应对守则(或fal):

$headers = get_headers( http://www.google.com );
$code = $headers ? intval(end(explode(   , $headers[0], 2))) : false;

Apart from that, there s also the matter of redirects: what do you do if you see a redirect? The server you queried might be OK, but the server you are redirected to might be down. If someone typed the URL in a browser they d be redirected and ultimately time out, while the one-step test would say everything s OK. What if there s a redirect loop? A browser would detect this and eventually time out, but you need to write quite a lot of code to do the same.

因此,归根结底,URL并不像唯一的停火解决办法,因为它以透明的方式这样做。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签