웹 사이트 ($ 호스트)에서 문자열 ($ 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);
?>
코드의 세 번째 반복에는 배열을 통해 실행되지 않는 foreach가 있습니다. 배열의 첫 번째 사이트 만 검사됩니다. 내가 얻는 유일한 메시지는 "Undefined variable : str in /srv/www/php/php_testing/monitor.php on line 25"입니다. 내 실수가 어디 있는지 모르겠습니다. – Havock