2011-10-20 3 views
0

웹 사이트 ($ 호스트)에서 문자열 ($ find)을 검사하는 스크립트가 있습니다. 문자열이 없으면 아무 일도 일어나지 않습니다. 문자열이 발견되지 않으면 전자 메일이 미리 설정된 전자 메일 주소로 전송됩니다.PHP 스크립트에 배열 추가

내가 가지고있는 문제는 URL의 배열을 가져야한다는 것이고 두 번째 텍스트 배열을 믿는다는 것입니다. 배열의 텍스트는 배열의 URL과 일치해야합니다.

아마도 URL과 텍스트를 텍스트 파일에 저장하는 것이 더 나은 해결책 일 수 있습니다.

지금은 단일 도메인에서 작업중인 스크립트입니다. 장소에 배열

<?php 
    $host = 'www.my-domain.com'; 
    $find = 'content on my page'; 

    function check($host, $find) { 
     $fp = fsockopen($host, 80, $errno, $errstr, 10); 
     if (!$fp) { 
      echo "$errstr ($errno)\n"; 
     } else { 
      $header = "GET/HTTP/1.1\r\n"; 
      $header .= "Host: $host\r\n"; 
      $header .= "Connection: close\r\n\r\n"; 
      fputs($fp, $header); 
      while (!feof($fp)) { 
       $str.= fgets($fp, 1024); 
      } 
      fclose($fp); 
      return (strpos($str, $find) !== false); 
     } 
    } 


    function alert($host) { 
     mail('[email protected]', 'Monitoring', $host.' down'); 
    } 

    if (!check($host, $find)) alert($host); 

    ?> 

새로운 코드 :

$hostMap = array(
    'www.my-domain.com' => 'content on site', 
    'www.my-domain2.ca' => 'content on second site', 
); 

foreach ($hostMap as $host => $find) 
{ 
     function check($host, $find) 
     { 
       $fp = fsockopen($host, 80, $errno, $errstr, 10); 
       if (!$fp) 
       { 
         echo "$errstr ($errno)\n"; 
       } else { 
         $header = "GET/HTTP/1.1\r\n"; 
         $header .= "Host: $host\r\n"; 
         $header .= "Connection: close\r\n\r\n"; 
         fputs($fp, $header); 
         while (!feof($fp)) { 
           $str.= fgets($fp, 1024); 
         } 
         fclose($fp); 
         return (strpos($str, $find) !== false); 
       } 
     } 

     function alert($host) 
     { 
       mail('[email protected]', 'Website Monitoring', $host.' is down'); 
     } 

     print $host; 
     print $find; 

//if (!check($host, $find)) alert($host); 

     if(!check($host, $find)) 
     { 
       alert($host); 
     } 
} 

?> 

는 foreach는 (여기

ini_set('display_errors', true); 
     $hostMap = array(
     'www.my-domain.com' => 'content on site', 
     'www.my-domain2.ca' => 'content on second site', 
    ); 

      function check($host, $find) 
      { 
        $fp = fsockopen($host, 80, $errno, $errstr, 10); 
        if (!$fp) 
        { 
          echo "$errstr ($errno)\n"; 
        } else { 
          $header = "GET/HTTP/1.1\r\n"; 
          $header .= "Host: $host\r\n"; 
          $header .= "Connection: close\r\n\r\n"; 
          fputs($fp, $header); 
          while (!feof($fp)) { 
            $str.= fgets($fp, 1024); 
          } 
          fclose($fp); 
          return (strpos($str, $find) !== false); 
        } 
      } 

      function alert($host) 
      { 
        mail('[email protected]', 'Website Monitoring', $host.' is down'); 
      } 

      print $host; 
      print $find; 

    //if (!check($host, $find)) alert($host); 
    foreach ($hostMap as $host => $find) 
    { 

      if(!check($host, $find)) 
      { 
        alert($host); 
      } 
    } 

    ?> 

이 경우 다른 사람이 원하는에 작업 배열에 최종 코드의 외부 기능을 이전 이 같은 해결책은

function check($host, $find) 
    { 
     $fp = fsockopen($host, 80, $errno, $errstr, 10); 
     if (!$fp) 
      { 
          echo "$errstr ($errno)\n"; 
         } else { 
          $header = "GET/HTTP/1.1\r\n"; 
          $header .= "Host: $host\r\n"; 
          $header .= "Connection: close\r\n\r\n"; 
          fputs($fp, $header); 
          while (!feof($fp)) { 
            $str.= fgets($fp, 1024); 
          } 
          fclose($fp); 
          return (strpos($str, $find) !== false); 
         } 
    } 

function alert($host) 
    { 
     $headers = 'From: Set your from address here'; 
     mail('[email protected]', 'Website Monitoring', $host.' is down' $headers); 
    } 

$hostMap = array(
'www.my-domain.com' => 'content on site', 
'www.my-domain2.com' => 'content on second site', 
); 

    //if (!check($host, $find)) alert($host); 
    foreach ($hostMap as $host => $find) 
    { 

      if(!check($host, $find)) 
      { 
        alert($host); 
      } 
    } 
unset($host); 
unset($find); 

?> 
+0

코드의 세 번째 반복에는 배열을 통해 실행되지 않는 foreach가 있습니다. 배열의 첫 번째 사이트 만 검사됩니다. 내가 얻는 유일한 메시지는 "Undefined variable : str in /srv/www/php/php_testing/monitor.php on line 25"입니다. 내 실수가 어디 있는지 모르겠습니다. – Havock

답변

2
$hostMap = array(
    'www.my-domain.com' => 'content on my page', 
    /* etc. */ 
); 

foreach($hostMap as $host => $find) 
{ 
    if(!check($host, $find)) 
    { 
     alert($host); 
    } 
} 

그러나,주의하십시오. SwiftMailer과 같이 좀 더 특수화 된 메일 라이브러리를 찾아 볼 수도 있습니다.

다른 한편으로는 - 하나의 전자 메일 주소와 동일한 전자 메일 주소를 보내는 중일 것입니다 - 실패한 도메인을 배열에 저장하고 완료 한 후 전자 메일로 모두 메일로 보낼 수도 있습니다 물론 확인.

+0

나는 이것을 작동 시키려고 노력해 왔지만, 내가 생각해 낸 것은 배열의 첫 번째 사이트를 벗어나지 않는 것 같다. 위 질문에 새 코드를 추가했습니다. – Havock

+0

@ user973661 :'foreach' 루프 밖에서 함수를 정의해야합니다 ('foreach' 루프의 모든 반복문이 아닌). 사실, 기능을 재정의하는 것도 허용되지 않습니다. 이로 인해 치명적인 오류가 발생합니다. 스크립트의 시작 부분에'ini_set ('display_errors', true)'를 넣어 화면에 오류를 표시하지 않는다고 가정합니다. 그렇지 않으면이 오류에 대한 알림을 받아야합니다. 프로덕션 시스템이 아닌 개발 시스템에서만 오류를 표시하는지 확인하십시오. –

+0

display_errors를 추가하고 foreach 외부에서 함수를 이동했습니다 (위 코드 추가). 정의되지 않은 변수가 있는데 여전히 하나의 사이트 만 확인합니다. – Havock

1

다차원 배열에 모든 것을 저장하고 코드의 전체 작업 섹션 주위에 반복기를 넣을 수 있습니다. 확인하려는 도메인의 양에 따라 - - PHP의 기본 mail()와 메일 대량 메일 링 순차적하는 것은 매우 효율적이지 않다

$list_of_sites[0]["url"] = blah; 
$list_of_sites[0]["text"] = blah; 
$list_of_sites[1]["url"] = blah; 
$list_of_sites[1]["text"] = blah; 

foreach($list_of_sites as $site){ 
    $url = $site["url"]; 
    $text = $site["text"]; 

    check($url, $text); 
} 
관련 문제