#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    UK
    Posts
    179
    Rep Power
    12

    PHP while loop time control


    Hi

    I am building a script that pulls lines from a file which are URL references and then checks the URL's for their HTTP response codes. The trouble is some of the URLs are non existent and the loop time takes quite a while.

    I have looked at various flow control methods but I haven't found what I am looking for. What I would like to do is allow each while loop cycle to have a time limit (not the entire script execution). For example, I would like to allow a loop to run for say 2 seconds and then skip to the next cycle if the time limit is reached.

    Here is what I have

    Code:
                     while (($url = fgets($handle, 1024)) !== false) 
        
                            {
            
                             echo "<tr><td><a href=\"".$url."\">".$url."</td><td>";
            
                             $array = (get_headers($url)); 
       
                             $response_code = $array[0];
        
                             if (!$response_code) {$response_code = "000";}     
                     
                                echo $response_code."</td><td>".$time."</td></tr>";         
    
                             }
    Thanks in advance for your suggestions.
  2. #2
  3. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,945
    Rep Power
    898
    Originally Posted by tstolber
    For example, I would like to allow a loop to run for say 2 seconds and then skip to the next cycle if the time limit is reached.
    This isn't exactly the same thing, but the only lines that would cause any hanging seem to be related to the HTTP Streams. You could set a low socket timeout prior to the loop.

    PHP Code:
    $settings = array('timeout' => 2);
    $streams = array('http' => $settings);
    stream_context_set_default($streams); 
    This should set the timeout to 2 seconds and applies to all functions using the HTTP Stream Wrapper.
    Verify and sanitize ALL USER DATA.

    And, to steal a quote from jeremy, "Explain your problem instead of asking how to do what you decided was the solution." Chances are someone on the forums will know a better or more efficient way to do what you're trying to accomplish.

    Avatar: Stolen by me, shown to me by patrick.


IMN logo majestic logo threadwatch logo seochat tools logo