2012-06-26 2 views
0

좋아, 내가 너무 명확하지 않은 질문 앞에 게시 했으므로 다시 시도하겠습니다. 배치마다 5 개의 일괄 처리로 웹 사이트를 구성하려고합니다. 각 사이트마다 다른 IP 주소가 있습니다.IP 주소별로 PHP 정렬 URL

이렇게하려면 각 URL에 대해 IP를 즉시 받아 와서 사이트를 배치 당 5 개 사이트의 배치로 구성해야합니다. 각 사이트에는 고유 한 IP 주소가 있습니다.

IP 주소가 여러 개인 경우 다음 번에 표시해야합니다.

누구든지 내 문제를 해결할 수 있습니까?

사이트와 IP가있는 배열이 있습니다. 현재 이곳에

if(isset($Organize_List)){ 
$List = explode("\n", $Organize_List); 
$IP_Array = array(); 
     foreach($List as $SIte){ 
    $Ip = gethostbyname(trim($SIte)); 
     if(preg_match('/^\d+$/', $Ip[1])){ 
     $IP_Array[$Ip][] = $SIte.'<br />'; 
       } 
} 

가의 까다로운을 얻을 수있는 곳입니다 :

여기 내 코드입니다.

내 문제는 어떻게 일괄 처리 당 5 개 사이트의 일괄 처리로 구성하고 각각 고유 한 IP 주소가 있음을 나타냅니다.

+2

'parse_url','gethostbyname','foreach'. 시작하기에 충분할 정도로 실제 코드로 문제가 발생하면 다시 돌아 오십시오. – Wrikken

+0

나는 그 모든 일을했다, 내 문제는 각 사이트마다 정보를 얻지 못하고 있지만, 내가 위에서 언급 한 저택에서 그들을 모두 정렬. – RmH

+0

코드를 입력하십시오 .. – sarnold

답변

0
/* your base data */ 
$domains = array(/* your data */); 

/* a list of domains per ip number, like $domain_by_ip['64.34.119.12'] = 'stackoverflow.com' */ 
$domain_by_ip = array(); 

/* a list counting number of domains by ip number */ 
$ip_count = array(); 

/* a list of domains we faild to fetch ip number for */ 
$failed = array(); 

/* loop through all doains */ 
foreach($domains as $current_domain) 
{ 
    /* fetch the A record for all domains */ 
    $current_dns_record = dns_get_record($current_domain, DNS_A); 

    /* if there is a dns record */ 
    if($current_dns_record) 
    { 
     /* fetch ip from result */ 
     $current_ip = $current_dns_record[0]['ip']; 

     /* thos row is not needed, but php may triggering a warning oterhwise */ 
     if(!isset($domain_by_ip[$current_ip])) $domain_by_ip[$current_ip] = array(); 
     if(!isset$ip_count[$current_ip])) $ip_count[$current_ip] = array(); 

     /* add domain to the list by ip */ 
     $domain_by_ip[$current_ip][] = $current_dns_record; 

     /* count up the count of domains on this ip */ 
     $ip_count[$current_ip]++; 
    } 
    else 
    { 
     /* if there was no dns record, put this domain on the fail list */ 
     $failed[] = $current_domain; 
    } 
} 

/* create a list for storing batches */ 
$batches = array(); 

/* as long as we have ip-numbers left to use */ 
while($ip_count) 
{ 
    /* create a list for storing current batch */ 
    $current_batch = array(); 

    /* sort ip-numbers so we take the ip-numbers whit most domains first */ 
    arsort($ip_count); 

    /* take the top 5 ip-numbers from the list */ 
    $current_batch_ip_list = array_slice(array_keys($ip_count), 0, 5); 

    /* foreach of thous 5 ip-numbers .. */ 
    foreach($current_batch_ip_list as $current_ip) 
    { 
     /* move one domain from the domain by ip list to the current batch */ 
     $current_batch[] = array_pop($domain_by_ip[$current_ip]); 

     /* count down the numbers of domains left for that ip */ 
     $ip_count[$current_ip]--; 

     /* if there is no more domains on this ip, remove it from the list */ 
     if($ip_count[$current_ip] == 0) 
     { 
     unset($ip_count[$current_ip]); 
     } 
    } 

    /* add current batch to the list of batches */ 
    $batches[] = $current_batch; 
} 
+0

빠른 재생을위한 첫 번째 감사. 그래도 코드 팝업이이 오류에서 빠져 나옵니다. "DNS 쿼리에 실패했습니다"gethostbyname을 사용하면이 작업이 모두 동일하게 작동합니까? – RmH

+0

dns는 ip-adresses를 가져 오는 데 사용되는 방법입니다. gethostbyname은 '$ current_dns_record ='- 행과 '$ current_ip ='- 행을 바꿔야합니다. –

+0

멋진 편집. 모두 같은 메시지가 나온다. 정의되지 않은 색인 : 199.96.156.205 – RmH