PHP脚本执行超时的解决办法
php.ini 中缺省的最长执行时间是 30 秒,虽然可以通过调整 php.ini 中 max_execution_time的值来达到目的,但有些情况是没有条件修改php.ini的,如何解决这个问题呢。
一种方法是在 PHP 脚本中加入
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4890')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4890>ini_set('max_execution_time', '0');
将运行时间设置成0(无限值);
另一种方法是在命令行下执行脚本,使用命令行执行脚本时,最大运行时间被设置为了无限值。
修改php.ini的脚本执行时间限制
编辑php.ini,修改max_execution_time值:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3202')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3202>max_execution_time=500
//此修改需要重新加载php.ini,需要重启web服务器生效。
通过.htaccess 文件设置脚本执行时间
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5493')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5493>php_value max_execution_time 500
在脚本中设置执行的最大时间
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5470')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5470>ini_set('max_execution_time', 500);
用php的函数取消脚本的时间限制
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5341')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5341>set_time_limit(0);
set_time_limit用来设置脚本的超时时间,此函数规定从该句运行时起程序必须在指定秒数内运行结束,超时则程序出错退出。
下面是一个例子. 有10000条数据, 要修改其中某些数据, 运用PHP分步执行处理, 代码如下:
action.php
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9859')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9859><?php
$stid = isset($_GET['stid'])?$_GET['stid']:0;
$endid = $stid 100;
$maxid = 10000;
function dosomething(){
//要时间比较多的操作
……
}
$sql_string=”select * from `table` where id>’$stid’ and id<=’$endid’ order by id”;
$datas = getdata_bysql($sql_string);
foreach($datas as $data){
//处理数据
…..
echo $id.” 处理完成. <br />”;
if($id>=$maxid){exit;}
}
if($stid<=$maxid){
$stid = $stid 100;
$url=”action.php?stid=$stid”;
echo $url;
echo ‘<script language=”javascript”>location=”‘.$url.’”; </script>’;
}
?>
其中的dosomething()是一个耗时操作. 这里我们通过限制id范围来减少运行时间, 运行完后通过javascript的跳转来自动运行下一步
现在dedecms生成html页面时就是这样做的哦。
您可能感兴趣的文章:
解决php运行超时的方法
Fatal error: Maximum execution time of 30 错误解决办法
php 30秒超时限制怎么解决
PHP脚本执行超时的解决办法
PHP程序时出现 Fatal error: Maximum execution time of 30 seconds exceeded in 提示
php定时计划任务 ignore_user_abort的用法举例
Maximum execution time of 30 seconds exceeded in
asp.net 执行sql超时的解决方法分享
asp脚本超时的解决办法
对于PHP-FPM参数的理解