sleep() - php 选项信息函数
sleep()
(PHP 4, PHP 5, PHP 7)
延缓执行
说明
sleep(int $seconds): int程序延迟执行指定的$seconds的秒数。
参数
$seconds暂停的秒数。
返回值
成功时返回 0,错误时返回FALSE
。
如果函数的调用被一个信号中止,sleep()会返回一个非零的值。在Windows上,该值总是192(即Windows API常量WAIT_IO_COMPLETION
的值)。其他平台上,该返回值是剩余未sleep的秒数。
错误/异常
如果指定的$seconds是负数,该函数会产生一个E_WARNING
级别的错误。
更新日志
版本 | 说明 |
---|---|
5.3.4 | 在PHP 5.3.4之前,Windows平台下无论sleep()是否成功调用,总是会返回一个NULL 。 |
范例
Example #1sleep()的例子
该例子会在休眠10秒后输出。
05:31:23 05:31:33
参见
usleep()
以指定的微秒数延迟执行time_nanosleep()
延缓执行若干秒和纳秒time_sleep_until()
使脚本睡眠到指定的时间为止。set_time_limit()
设置脚本最大执行时间
This may seem obvious, but I thought I would save someone from something that just confused me: you cannot use sleep() to sleep for fractions of a second. This: will not work as expected. The 0.25 is cast to an integer, so this is equivalent to sleep(0). To sleep for a quarter of a second, use:
re: "mitigating the chances of a full bruit force attack by a limit of 30 lookups a minute." Not really - the attacker could do 100 requests. Each request might take 2 seconds but it doesn't stop the number of requests done. You need to stop processing more than one request every 2 seconds rather than delay it by 2 seconds on each execution.
Maybe obvious, but this my function to delay script execution using decimals for seconds (to mimic sleep(1.5) for example):
Diego Andrade's msleep function is not compatible with php7's `strict_types`, cast the usleep parameter to int, and it will be, usleep((int)($time * 1000000));
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.
Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), the sleep() function, database queries, etc. is not included when determining the maximum time that the script has been running.
If you are having issues with sleep() and usleep() not responding as you feel they should, take a look at session_write_close() as noted by anonymous on comments; "If the ajax function doesn't do session_write_close(), then your outer page will appear to hang, and opening other pages in new tabs will also stall."
it is a bad idea to use sleep() for delayed output effects as 1) you have to flush() output before you sleep 2) depending on your setup flush() will not work all the way to the browser as the web server might apply buffering of its own or the browser might not render output it thinks not to be complete netscape for example will only display complete lines and will not show table parts until the tag arrived so use sleep if you have to wait for events and don't want to burn to much cycles, but don't use it for silly delayed output effects!
This will allow you to use negative values or valuer below 1 second.
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.
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
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.
Always close your SQL connection and free the memory before using sleep( ) or you will be needlessly holding a SQL connection for [xx] seconds, remember that a shared hosting environment only allows max 30 SQL connections at the same time.
Simple function to report the microtime since last called or the microtime since first called. $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
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.
Another reason for not to abuse sleep( ) is that along with the maximum of 30 sql connections, a shared hosting environment usually limits the number of processes to 20, if your website has many users online and you put sleep( ) everywhere in the code, your server will throw a 508 error (resource limit reached) and will stop serving your website.
To use float or int numbers function pause($seconds) { usleep($seconds * 1000000); } pause(0.25);
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.
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 :
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)