php sleep timeout,PHP 延缓执行

这篇博客探讨了PHP的sleep函数在不同场景下的使用,包括如何在输出到浏览器时进行延迟,防止暴力攻击,以及处理信号中断。示例代码展示了如何确保输出及时、控制脚本延迟和实现中断后的继续睡眠。还提到了在多会话环境下的行为,并给出了实用的函数和技巧来优化sleep的使用。
摘要由CSDN通过智能技术生成

用户评论:

joshmeister at gmail dot com (2012-10-16 22:51:51)

Here is a simplified way to flush output to browser before completing sleep cycle.  Note the buffer must be "filled" with 4096 characters (bytes?) for ob_flush() to work before sleep() occurs.

ob_implicit_flush(true);$buffer=str_repeat(" ",4096);

echo"see this immediately.
";

echo$buffer;ob_flush();sleep(5);

echo"some time has passed";?>

barlow at fhtsolutions dot com (2011-09-24 06:33:57)

You should put sleep into both the pass and fail branches, since an attacker can check whether the response is slow and use that as an indicator - cutting down the delay time. But a delay in both branches eliminates this possibility.

code {@} ashleyhunt [dot] co [dot] uk (2011-07-30 06:31:12)

A really simple, but effective way of majorly slowing down bruit force attacks on wrong password attempts.

In my example below, if the end-user gets the password correct, they get to log in at full speed, as expected. For every incorrect password attempt, the users response is delayed by 2 seconds each time; mitigating the chances of a full bruit force attack by a limit of 30 lookups a minute.

I hope this very simple approach will help make your web applications that little bit more secure.

Ashley

if($uid=user::check_password($_REQUEST['email'],$_REQUEST['password'])) {

returnself::authenticate_user($uid);

}

else {// delay failed output by 2 seconds

// to prevent bruit force attackssleep(2);

returnself::login_failed();

}

}?>

soulhunter1987 at post dot ru (2010-08-18 00:49:51)

Since sleep() can be interrupted by signals i've made a function which can also be interrupted, but will continue sleeping after the signal arrived (and possibly was handled by callback). It's very useful when you write daemons and need sleep() function to work as long as you 'ordered', but have an ability to accept signals during sleeping.

{$start=microtime(true);

for ($i=1;$i<=$seconds;$i++) {

@time_sleep_until($start+$i);

}

}?>

toddjt78 at msn dot com (2010-05-09 19:39:37)

Simple function to report the microtime since last called or the microtime since first called.

global$first_called;

global$last_called;$now_time=microtime(true);

if ($last_called===null) {$last_called=$now_time;$first_called=$now_time;

}

if ($total) {$time_diff=$now_time-$first_called;

} else {$time_diff=$now_time-$last_called;

}

if ($reset)$last_called=$now_time;

return$time_diff;

}?>

$reset  - if true, resets the last_called value to now

$total - if true, returns the time since first called otherwise returns the time since last called

jimmy at powerzone dot dk (2010-03-06 08:26:18)

Notice that sleep() delays execution for the current session, not just the script. Consider the following sample, where two computers invoke the same script from a browser, which doesn't do anything but sleep.

PC 1 [started 14:00:00]: script.php?sleep=10 // Will stop after 10 secs

PC 1 [started 14:00:03]: script.php?sleep=0 // Will stop after 7 secs

PC 2 [started 14:00:05]: script.php?sleep=0 // Will stop immediately

http://php.net/session_write_close may be used to address this problem.

mohd at Bahrain dot Bz (2009-12-16 01:12:36)

I hope this code will help somebody to solve the problem of not being able to flush or output the buffer to the browser (I use IE7).

It may work for you with just [ echo str_repeat(".", 4096); ] and without even using ob_... and flush.

ob_start();ob_implicit_flush(true);//[ OR ] echo "..."; ob_flush(); flush();set_time_limit(0);

functionsleep_echo($secs) {$secs= (int)$secs;$buffer=str_repeat(".",4096);//echo $buffer."\r\n
\r\n";for ($i=0;$i

echodate("H:i:s",time())." (".($i+1).")"."\r\n
\r\n".$buffer."\r\n
\r\n";ob_flush();flush();sleep(1);//usleep(1000000);}

}sleep_echo(30);ob_end_flush();?>

f dot schima at ccgmbh dot de (2009-11-10 06:48:07)

Remember that sleep() means "Let PHP time to do some other stuff".

That means that sleep() can be interrupted by signals. That is important if you work with pcntl_signal() and friends.

Anonymous (2009-02-08 12:32:24)

This will allow you to use negative values or valuer below 1 second.

{$seconds=abs($seconds);

if ($seconds<1):usleep($seconds*1000000);

else:sleep($seconds);

endif;

}?>

webseos at gmail dot com (2008-08-26 21:29:15)

This is a critical thing to use time delay function as sleep() Because a beginner can find that this is not working and he/she will see that all output appearing at a time.

A good way to implement this is by using the function -  ob_implicit_flush() then you don't need to use flush() function explicitly.

A sample code :

ob_implicit_flush(true);

for($i=0;$i<5;$i++)

{$dis=<<$i

DIS;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值