Looping code while function loads

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

nutkenz
Forum Contributor
Posts: 155
Joined: Tue Jul 19, 2005 12:25 pm

Looping code while function loads

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.........
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

interesting class. i'll have to play around with it.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

nice find feyd!!

EDIT: I guess this is not supported by all browsers... yet...
nutkenz
Forum Contributor
Posts: 155
Joined: Tue Jul 19, 2005 12:25 pm

Post 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.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Last edited by CoderGoblin on Wed Sep 21, 2005 6:56 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

JS Timing event link at w3schools.com
nutkenz
Forum Contributor
Posts: 155
Joined: Tue Jul 19, 2005 12:25 pm

Post 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.
nutkenz
Forum Contributor
Posts: 155
Joined: Tue Jul 19, 2005 12:25 pm

Post 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.
Post Reply