简体   繁体   English

500执行简单的PHP循环程序时出现内部服务器错误?

[英]500 internal server error when execute a simple PHP loop program?

<?php
set_time_limit(600);
while (1){
usleep(30000000);
}
?>

The php program above causing the 500 internal server error. 上面的php程序导致500内部服务器错误。 The problem is , I have set the time limit as 600 -> 10 minutes. 问题是,我将时间限制设置为600-> 10分钟。 And I am expecting it will run for ten minutes, for 30 second each , it will run a while loop for 1 time, and keep the activity to 10 minute. 我预计它将运行10分钟,每次运行30秒,它将运行while循环1次,并将活动保持在10分钟。 However, it return 500 internal server error at round 2 minutes. 但是,它在第​​2分钟返回500个内部服务器错误。 What is the root cause / how to fix the problem ? 根本原因是什么/如何解决该问题? thanks 谢谢

I spun your code up on my own Linux-based web-server. 我在我自己的基于Linux的Web服务器上启动了您的代码。 I used a Linux server rather than IIS simply because I thought it would give me more debugging information. 我使用Linux服务器而不是IIS只是因为我认为它将为我提供更多调试信息。 Your code for me works just fine. 您的代码对我来说效果很好。 It doesn't time out. 它不会超时。 I would then have to say that there is something specific to IIS. 然后,我不得不说有一些特定于IIS的东西。 I would check the Event Log. 我会检查事件日志。 A 500 error should have something interesting in there. 500错误应该有一些有趣的地方。 To get to the event log ( http://msdn.microsoft.com/en-us/library/ms524984(v=vs.90).aspx ): 要获取事件日志( http://msdn.microsoft.com/zh-cn/library/ms524984(v=vs.90).aspx ):

  • From the Start menu, click Run. 在开始菜单上,单击运行。 In the Open box, type eventvwr . 在“打开”框中,键入eventvwr Click OK. 单击确定。
  • Event Viewer is listed under the System Tools node. “事件查看器”在“系统工具”节点下列出。

I would suggest, if you need this program to wait for 10 minutes, either go into your PHP.ini file (if that's the problem) and up the max_execution_time by A LOT, or as an alternative, let the page sit for 10 seconds and then reload the same page with header() . 我建议,如果您需要该程序等待10分钟,请进入您的PHP.ini文件(如果出现问题),然后将max_execution_time放很多,或者将页面静置10秒钟,然后然后使用header()重新加载同一页面。 The advantage to this approach is that you aren't killing EXTRA CPU cycles by just spinning and locking everything up. 这种方法的优点是您不会仅仅通过旋转和锁定所有内容来杀死EXTRA CPU周期。 You are instead just re-querying your server and using a persistent cookie: 相反,您只是重新查询服务器并使用持久性cookie:

<?php
echo "Time Slept: ".$_COOKIE['total_time_slept']; //persistent variable you can use to measure how much time has elapsed
if($_COOKIE['total_time_slept'] < 1000) //if less than 10 minutes
{     
    $page = $_SERVER['PHP_SELF'];
    $sec = 10;        
    setcookie('total_time_slept', $_COOKIE['total_time_slept']+$sec); //append that time to the counter
    header("Refresh: $sec; url=$page"); //reload this page every 10 seconds
}
?>

I had a similar problem with usleep on a LAMP installation where PHP run in safe-mode . 我在LAMP安装中的usleep上遇到了类似的问题,其中PHP以安全模式运行

So my suggestion is check if your PHP run on safe-mode . 所以我的建议是检查您的PHP是否在安全模式下运行 If that's the case try using the sleep function (although it won't help if you want to loop each 0.001s for instance). 如果是这种情况,请尝试使用sleep函数(尽管例如,您想循环每个0.001s不会有帮助)。

Note also that usleep doesn't work on PHP Windows platforms until PHP 5. 另请注意,直到PHP 5为止, usleep才在PHP Windows平台上工作。

I know this thread is old but I hope this check will help the others. 我知道这个线程很旧,但我希望此检查对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM