Page 1 of 2

Looping code while function loads

Posted: Tue Sep 20, 2005 2:16 pm
by nutkenz
Ok, I'm not sure if this is even possible... But I want to output dots the the browser for as long as one of the functions (whois lookup) keeps loading (which is usually over 30 seconds).

Here is my code:

Code: Select all

// Reset counters
$timeoutcounter = 0;
$retrycounter = 0;

while (empty($isavail)) // If isavail is empty, the WHOIS has not yet been completed
   {
      echo "LOOP #$timeoutcounter ";//REMOVE - FOR TESTING ONLY
       // If 5 seconds elapsed, send a dot (or any other character)
       if ($timeoutcounter == 5)
       {
           echo ". <br>Available: $isavail <br>Retry: $retrycounter <br>Domain: $fulldomain<br>";
           $timeoutcounter = 0;
       }
       
       // Send the WHOIS after x seconds
       if ($retrycounter == 1)
       {
            // start buffering the output
            ob_start();
            
            include ("whois.inc");
            $whoisresult = lookup($fulldomain);
            
            while (empty($isavail))

            {
                // Check if domain is available
                $isavail = $whoisresult[0]; // Contains "1" is it's available, blank if it's registered
                echo "<br>CHECK COMPLETE: $isavail <br> Result: $whoisresult[0]<br>";//REMOVE - FOR TESTING ONLY
            }
            
            // Flush buffer once loop has been broken
            ob_flush();
            flush();
            
       }

       // Get WHOIS result
       $isavail = $whoisresult[0]; // Contains "1" is it's available, blank if it's registered

       // Timeout executing
       sleep(1);

       // Increase counter
       $timeoutcounter++;
       $retrycounter++;
   }

I've been scrapping and re-coding all day and I just can't figure it out... What happens now is this:

Gelieve te wachten terwijl we de beschikbaarheid van uw domein controleren ...LOOP #0 LOOP #1

So it loops until the function starts loading, and then it waits until the loading is complete before doing anything else instead of repeating the loop until the buffer can be sent (which is when the WHOIS is done).

Any help would be greatly appreciated.

Posted: Tue Sep 20, 2005 3:07 pm
by s.dot
It cannot be done using php alone, as it is not a multi-threaded language. However, you could use a javascript function, or an iframe.

Posted: Tue Sep 20, 2005 3:46 pm
by Jenk
Or do what some websites do and load a GIF of a knight rider esque bar in a second window and close it after a set time :P

Posted: Tue Sep 20, 2005 3:49 pm
by ryanlwh
Jenk wrote:Or do what some websites do and load a GIF of a knight rider esque bar in a second window and close it after a set time :P
as far as I know that's also javascript (along with META tags I suppose).... PHP alone cannot control how the page behave after it's generated. What you need is client side scripting with javascript.

Posted: Tue Sep 20, 2005 3:50 pm
by Jenk
Of course it is, but I was referring more to the comic value of it not actually being a progres bar, just an animation :)

Posted: Tue Sep 20, 2005 4:33 pm
by feyd
you can do it with php... ;)

http://www.phpclasses.org/browse/package/1704.html


I'm starting to link to this pretty regularly.........

Posted: Tue Sep 20, 2005 4:58 pm
by s.dot
interesting class. i'll have to play around with it.

Posted: Tue Sep 20, 2005 5:21 pm
by John Cartwright
feyd wrote:you can do it with php... ;)

http://www.phpclasses.org/browse/package/1704.html


I'm starting to link to this pretty regularly.........
Maybe this belongs in the usefull posts thread :wink:

Posted: Tue Sep 20, 2005 6:45 pm
by ryanlwh
nice find feyd!!

EDIT: I guess this is not supported by all browsers... yet...

Posted: Wed Sep 21, 2005 4:14 am
by nutkenz
By the way, in case I'd do it with Javascript, would that keep the browser open? Because if it doesn't receive anything within 30 seconds it always stops loading... Just wondering about this as I'll probably try the PHP-only solution first.

Posted: Wed Sep 21, 2005 4:19 am
by n00b Saibot
nutkenz wrote:By the way, in case I'd do it with Javascript, would that keep the browser open? Because if it doesn't receive anything within 30 seconds it always stops loading... Just wondering about this as I'll probably try the PHP-only solution first.
You can try XMLHTTPRequest object for dynamic updation without refresh. its cross-browser too.

Posted: Wed Sep 21, 2005 6:47 am
by CoderGoblin
Long time since I looked into this but this code (influenced from this board a long time ago, can't find the topic now though) may also help without using HTTPRequests...

Code: Select all

<html>
  <head>
    <title>Information</title>
    <script language="javascript">
    <!--
    function getPageItem(itemID)
    {
        if (document.getElementById) return document.getElementById(itemID);
        if (document.all) return document.all[itemID];
        return null;
    }
    
    <!--
    function slUpdateSpan(newHtml)
    {
        var span_obj = getPageItem("idUpdateableSpan");
        if (span_obj) {
          if (span_obj.style.display != "inline") {
            span_obj.style.display = "inline";
          }
          span_obj.innerHTML = newHtml;
        }
    }
    
    function killUpdateSpan()
    {
        var span_obj = getPageItem("idUpdateableSpan");
        if (span_obj) span_obj.style.display = "none";
    }
    -->
    </script>
  </head>
  <body>
    <div id="idUpdateableSpan"
           style="position: absolute;
                    top: 40%; left: 35%;
                    width: 30%;
                    height: 80px;
                    filter:alpha(opacity=80);
                    -moz-opacity:80%;
                    margin:0;
                    padding:15px;
                    background-color:#eee;
                    border:1px solid #333;
                    text-align: center;
                    z-index:1000;
                    display:none;
                    cursor:default;
                    "></div>
    <script language="JavaScript">
    <!--
       slUpdateSpan("Getting Information...<br><br>");
    //-->
    </script>

    <?php include "getinformation.php" ?>

    <script language=Javascript>
        <!--
        killUpdateSpan();
        //-->
    </script>
 
    <?php echo($result); ?>
  </body>
</html>
If outputting the page as it goes you could add...

Code: Select all

echo("    <script language=\"JavaScript\">
    <!--
       slUpdateSpan(\"".$msg."...<br><br>\");
    //-->
    </script>");
flush();
after each change to the message you want to output.

Of course this doesn't solve the problem that a single php command can take a long time to process. Maybe javascript could help in this instance ( a timer calls the slUpdateSpan function) but unsure.

On a side note, If there is sufficient interest in this I will try to write a more descriptive topic with working examples.

Posted: Wed Sep 21, 2005 6:54 am
by CoderGoblin
JS Timing event link at w3schools.com

Posted: Wed Sep 21, 2005 2:44 pm
by nutkenz
I tried HV HTTP Multipart, but this doesn't work for PHP pages I think, it's only meant for HTML pages as far as I can tell. Either way, it completely broke the page.

Posted: Wed Sep 21, 2005 2:49 pm
by nutkenz
CoderGoblin wrote:Of course this doesn't solve the problem that a single php command can take a long time to process
Well, it's mainly due to the .NL whois check, it just takes too long for that server to respond. Other domains are usually finished within 1 or 2 seconds, but this one can take over 30.

Does the Javascript above keep the browser alive? If not, I'll still be unable to get WHOIS checks which take 30+ seconds to work. By the way, I already tried using set_time_limit(600); in both the main page and the function itself. PHP safe mode is disabled and max_execution_time is also set to 600 so it must be the browser itself giving up?

Thank you.