/    Sign up×
Community /Pin to ProfileBookmark

Waiting in loops?

I’m pretty sure I’ve seen it somewhere, but it could have been a different language. But what I want to do is execute a loop, but wait 10 seconds before it loops again.

[code=php]
for ($i=0;$i<$count;$i++) {
/* statement to execute */

wait(10);
}
[/code]

Hopefully there is. Can someone show me the right function (as wait() isn’t it) ?

Jaelan

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@JayMApr 13.2005 — It is the [URL=http://ca3.php.net/sleep]Sleep()[/URL] function. ?

Regards,

Jay
Copy linkTweet thisAlerts:
@bokehApr 13.2005 — It is 'sleep()' but if you are going to use it to print individual things to the browser you need implicit_flush to be switched on and output_buffering to be '0' in your php config.
Copy linkTweet thisAlerts:
@JaelanauthorApr 13.2005 — Oh yeah...

Heh. Thanks bokeh for the further explanation of sleep()...that was a problem I was running into.

Jaelan
Copy linkTweet thisAlerts:
@bokehApr 13.2005 — This works on my machine with the settings I gave you for php.ini

[code=php]
<?php


// current time
echo date('h:i:s') . '<br />' . "n";


// sleep for 10 seconds
sleep(10);


// wake up !
echo date('h:i:s') . "n";

?>
[/code]


With implicit_flush turned off it still sleeps for 10 seconds but it doesn't send anything to the browser until the script has finished runing.
Copy linkTweet thisAlerts:
@96turnerriApr 14.2005 — it will if you use flush()
Copy linkTweet thisAlerts:
@bokehApr 14.2005 — it will if you use flush()[/QUOTE]
I'm afraid you are wrong! If implicit_flush is turned 'off' in php.ini the flush() function will not result in output being sent to the browser unless that output is sufficient to completely fill a tcp/ip packet.
Copy linkTweet thisAlerts:
@96turnerriApr 14.2005 — according to the manual i have not tested this, but flush has no requirements or prequisites

implicit flush
FALSE by default. Changing this to TRUE tells PHP to tell the output layer to flush itself automatically after every output block. This is equivalent to calling the PHP function flush() after each and every call to print() or echo() and each and every HTML block.[/QUOTE]

so if implicit flush is true

[code=php]<?php
echo "foo";
?>
2
<?php
echo "another";
?>[/code]

would be the same as
[code=php]<?php
echo "foo";
flush();
?>
2
<?php
echo "another";
flush();
?>[/code]


with implicit flush set to false i can do

[code=php]<?php
for($x=0;$x<=10;$x++) {
echo $x;
flush();
sleep(1);
}
?>[/code]

and it will work fine, implicit flush is only for automated flushing after each PHP block
Copy linkTweet thisAlerts:
@bokehApr 14.2005 — i have not tested this,[/QUOTE]

The proof of the pudding is in the eating. Test and you will see I am correct. Don't forget it is not php that sends the packets, it's the server. If implicit_flush is switched off packets are not sent unless they are full.

Just to reassure you I tried your code on my server (listed below) and it only worked to your expectations with implicit_flush switched on.
Copy linkTweet thisAlerts:
@96turnerriApr 15.2005 — according to the manual[/QUOTE]
so the manual would be wrong there are prequisties for flush() you should submit your tests and get credited for it
Copy linkTweet thisAlerts:
@NogDogApr 16.2005 — Even if PHP is flushing, the web server may have its own caching going on.
Copy linkTweet thisAlerts:
@Stephen_PhilbinApr 16.2005 — Never mind all that. Why the hell would you want to slow your application down in the fist place?

That's just daft.
Copy linkTweet thisAlerts:
@bokehApr 16.2005 — The truth is I found this out by accident. I was trying to run a script that Nogdog wrote: [URL=http://www.charles-reace.com/flush.php]example here[/URL] and the original thread can be [URL=http://www.webdeveloper.com/forum/showthread.php?t=57753&highlight=page+loading]found here[/URL]. I couldn't get it to work on my server. I just waited the ten seconds with no text. Later I found the implicit_flush setting in php.ini and changed from the default off to on and the script worked as expected.
Copy linkTweet thisAlerts:
@bokehApr 16.2005 — Never mind all that. Why the hell would you want to slow your application down in the fist place? That's just daft.[/QUOTE]

Ok! Say You had a login script! A cracker is trying to log in by sending random usernames and passwords. The sleep() function could be used to slow down sign_in making brute force attacks more difficult.

A smart cracker could write his code to end the connection long before the sleep() is done, but that makes it a guessing game; how quickly can the connection be ended without accidently losing the successful connections the cracker wants? This is not an easy question to answer for the cracker because all of those concurrent connection attempts can significantly slow down all of the responses, even successful ones. Against this "smart" cracker there is going to be a limit; sleep(30) is probably going to be no more effective than sleep(5). In the end, using sleep() makes the code neccessary to brute force a password much more complicated and that in itself will eliminate some would-be crackers. For the others, it does slow them down somewhat.

So the short answer is that using sleep() after an authentication failure is _always_ better than not using sleep().

Best of all is to use sleep() after successful authentication too-- this would mean that the cracker _must_ wait the full sleep() period in every case and makes brute forcing a password require a very, very long time (years?). This probably wouldn't even bother users that much as it would only happen once per login.[/QUOTE]
Copy linkTweet thisAlerts:
@JaelanauthorApr 18.2005 — The reason I brought up the thread was because I wanted to a sort of slideshow w/ php. Thus, I had a loop that displayed a picture every 5 seconds (in theory)...well, my theory sucked, so I did it in javascript instead ?

But it is good to know about the uses of sleep() and authentication!
×

Success!

Help @Jaelan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...