我写了一个PHP脚本就是 查询数据库中的URL 是不是有效网址!每天晚上定时执行!但是我刚刚发现LINUX 进程中居然那个脚本到现在还运行!而且运行了4天!而且我不杀掉那个进程,那还是会继续运行!是我的脚本写了死循环?还是有问题!我贴出来给大家看看!指点下!<?php
set_time_limit(0);
$conn=mysql_connect('localhost','xxx','xxx');
$sql="select url from my_website ";
$result=mysql_db_query('xxx',$sql);
while($array=@mysql_fetch_array($result))//从数据中提取出URL 
{
$test=get_headers($array[url]);//截取HTTP开头 的200 304 等头属性
preg_match_all('/\s(\d+)\s/',$test[0],$url);
if($test==false)//无效网址
{
$sql1="update my_website set scanurl='false' where url=('$array[url]')";
$result1=mysql_db_query('xxxx',$sql1);
}
else//有效网址!
{
$newurl=$url[0][0];
$sql2="update my_website set scanurl=('$newurl')where url=('$array[url]')";
$result2=mysql_db_query('xxxx',$sql2);
}
sleep ( 0.5 );
}
?>

解决方案 »

  1.   

    现在对get_headers()函数有点怕怕的! 怀疑是超时的问题!但是也不可能这么长时间都没执行完呀?
      

  2.   

      下面就是运行的时间!
      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    22070 root      25   0 55516 9240 4920 R 99.9  0.2   5056:11 php
      

  3.   

    get_headers,是很不可预期的。
      

  4.   

    sleep(0.5);
    0.5 * 7000 = 3500s = 将近1小时
    get_headers要看你查看网址的速度,假定5s
    5* 7000 = 35000s 将近10小时看看你脚本的效率就知道了.
      

  5.   

    这个问题!现在搞好了!我自己重新定义了一个my_get_headers函数!function my_get_headers($url,$format=0) {
    $url_array=array();
           $url_info=parse_url($url);
           $port = isset($url_info['port']) ? $url_info['port'] : 80;
           @$fp=fsockopen($url_info['host'], $port, $errno, $errstr, 3);
           if($fp) {
               if(!$url_info['path']){
                             $url_info['path'] = "/";
                         }
                         if($url_info['path'] && !$url_info['host']){
                            $url_info['host'] = $url_info['path'];
                            $url_info['path'] = "/";
                         }
                         if( $url_info['host'][(strlen($url_info['host'])-1)] == "/" ){
                            $url_info['host'][(strlen($url_info['host'])-1)] = "";
                         }
                         if(!isset($url_array['scheme'])){
                             $url_array['scheme'] = "http"; //we always use http links
                            }
                         $head = "HEAD ".@$url_info['path'];
                         if( isset($url_info['query']) ){
                             $head .= "?".@$url_info['query'];
                            }
                            //print_r($url_info);
               $head .= " HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n";
               //echo $head;
                         fputs($fp, $head);
               while(!feof($fp)) {
                   if($header=trim(fgets($fp, 1024))) {
                       if($format == 1) {
                           $h2 = explode(':',$header);
                           // the first element is the http header type, such as HTTP/1.1 200 OK,
                           // it doesn't have a separate name, so we have to check for it.
                           if($h2[0] == $header) {
                               $headers['status'] = $header;
                           }
                           else {
                               $headers[strtolower($h2[0])] = trim($h2[1]);
                           }
                       }
                       else {
                           $headers[] = $header;
                       }
                   }
               }
               return $headers;
           }
           else {
               return false;
           }
       }