2014-11-20 2 views
0

저는 함수의 연속 루프를 막기 위해 die를 사용하는 스크립트를 가지고 있습니다. 하지만 내가 html 위에이 스크립트를 배치하면 html 스크립트가 중지되어 html 아래에 배치되지만 모든 변수는 실제 웹 사이트 아래에 표시됩니다. 어떻게하면 웹 사이트가 아닌 웹 사이트가되고 싶을 때 어떻게 되겠습니까? 아니면 죽을 때와 다른 방법이 있습니까?루프를 막기 위해 위에 PHP 변수를 보냅니다.

function QueryWhoisServer($whoisserver, $domain) { 
    $port = 43; 
    $timeout = 5; 
    $errm = "<p1>No connection could be made</p1>"; 
    $fp = @fsockopen($whoisserver, $port, $errno, $errstr, $timeout) or die($errm); 
    fputs($fp, $domain . "\r\n"); 
    $out = ""; 
    while (!feof($fp)) { 
     $out .= fgets($fp); 
    } 
    fclose($fp); 

    $res = ""; 
    if((strpos(strtolower($out), "error") === FALSE) && (strpos(strtolower($out), "not allocated") === FALSE)) { 
     $rows = explode("\n", $out); 
     foreach($rows as $row) { 
      $row = trim($row); 
      if(($row != ':') && ($row != '#') && ($row != '%')) { 
       $res .= $row."<br>"; 
      } 
     } 
    } 
    return $res; 
} 
+0

어디에서 기능을 나오고 싶습니까? –

답변

2

break 휴식 어떤 루프 중 키워드, 그냥 대신 die의 사용 :이 함수의 코드입니다.

중첩 루프가있는 경우 break는 가장 안쪽 루프 만 종료하므로주의하십시오. 이상하게도 php에서는 break (2)를 사용하여 두 개의 루프를 끊을 수 있습니다. 나는 그렇게하지 않을 것입니다.

1

die();은 모든 PHP 실행을 중지합니다. 실제로 그렇게하고 싶지는 않습니다.

대신 try-catch 구문을보고 예외를 던지고 잡아야합니다.

function QueryWhoisServer($whoisserver, $domain) { 
    try { 
     $port = 43; 
     $timeout = 5; 
     $errm = "<p1>No connection could be made</p1>"; 
     $fp = @fsockopen($whoisserver, $port, $errno, $errstr, $timeout); 

     if (!fp) { 
      throw new Exception ("Couldn't open socket."); 
     } 
     //after the exception is thrown, the rest of this block will not execute. 

     fputs($fp, $domain . "\r\n"); 
     $out = ""; 
     while (!feof($fp)) { 
      $out .= fgets($fp); 
     } 
     fclose($fp); 

     $res = ""; 
     if((strpos(strtolower($out), "error") === FALSE) && (strpos(strtolower($out), "not allocated") === FALSE)) { 
      $rows = explode("\n", $out); 
      foreach($rows as $row) { 
       $row = trim($row); 
       if(($row != ':') && ($row != '#') && ($row != '%')) { 
        $res .= $row."<br>"; 
       } 
      } 
     } 
     return $res; 
    } catch (Exception $e) { 
     //this block will be executed if any Exception is caught, including the one we threw above. 
     //you can handle the error here or rethrow it to pass it up the execution stack. 
     return ""; 
    } 

} 

PHP's Exceptions manual page

0

당신은 무한 루프를 방지하기 위해 제어 변수를 사용할 수 있습니다.

$end_condition = false; 
while (!$end_condition) 
{ 
    //do the job 
    if (conditions/are/met) 
    { 
     $end_condition = true; 
    } 
} 
관련 문제