如何通过正确切换在最长执行时间结束时给出用户定义的超时消息


How to give user defined timeout message when max execution time over by handeling properly

我正在设计自定义查询页面,用户可以在其中激发查询并获得所需结果。在我的数据库中,我有数以百万计的记录,显然执行大约需要10到15分钟的时间。超时页面终止后,php脚本下面的代码不会执行,所以它不会显示在设计和页脚下面。

请让我知道如何正确处理超时,以便整个页面执行并给出正确的用户定义的超时消息,如果执行时间超过,还可以在设计和页脚下面。

以下是我要做的…

 <form onsubmit="custom.php;" id="usrform" method="post">
     <textarea id="tarea" placeholder=" Example: select  malindex,malname,virustype,filetype,count from CT_DETECTIONS;" name="query" form="usrform" style="width:800px;height:100px;"></textarea>
   <br>
   <br><input type="submit" value="Submit Query" onclick="validate();">
</form> 
<br>  
<?php
//DB connection
if(isset($_POST['query'])&& $_POST['query']!='')
{
$x = $_POST['query']; 
$result = mysqli_query($con,$x);
if(!$result)
{ 
$msg='<b>Error: </b>Invalid query. Enter a valid query. Please refer to Database Schema <a href="tableinfo.html"><b style="color:black;">here</b> </a>';
}
else
{
$x="<b>Query: </b>" .$_POST['query'];
//fetching rows and drawing google chart table    
<?php echo $x;
} 
}?>
<div id="valid" style="color:red;">
  <?php echo $msg; ?>
</div> 
<div style="color:red">
  <lable id="myP">   </lable>
</div>
</div>
</div>
<div id="visualization" style="width:800px; height: 450px;color:black;margin-left:22px;"></div>
</div>
</div>

请让我知道如何处理由于超时而导致的页面终止

您可以使用set_time_limit(60*60)更改时间限制;www.php.net,它将把它设置为1h,或者看看这篇关于Stackoverflow的文章。

您可以从以下脚本中检查最大执行时间值:

if(ini_get('max_execution_time')</*Set value do you want(integer)*/{
//Overlimit
}
else{
//Your script
}

使用AJAX:

<?php
//DB connection
if(isset($_POST['query']))
{
    // MUST BE FIRST OUTPUT (nothing before)
    header('Content-type: text;charset=UTF-8');
    $x = $_POST['query'];
    $result = mysqli_query($con,mysqli_real_escape_string($con, $x));
    if (!$result)
    { 
        echo '<b>Error: </b>Invalid query. Enter a valid query. Please refer to Database Schema <a href="tableinfo.html"><b style="color:black;">here</b> </a>';
        exit();
    }
    //fetching rows and drawing google chart table    
    echo $x;
    exit();
}
?>
<form onsubmit="return submitUsrForm();" id="usrform" method="post">
     <textarea id="tarea" placeholder=" Example: select  malindex,malname,virustype,filetype,count from CT_DETECTIONS;" name="query" form="usrform" style="width:800px;height:100px;"></textarea>
   <br>
   <br><input type="submit" value="Submit Query" onclick="validate();">
</form> 
<br>
<b>Query: </b><span id="query"></span>
<div id="valid" style="color:red;">
</div> 
<div style="color:red">
  <lable id="myP">   </lable>
</div>
</div>
</div>
<div id="visualization" style="width:800px; height: 450px;color:black;margin-left:22px;"></div>
</div>
</div>
<script type="text/javascript">
function validate() { return true; }
function submitUsrForm() {
    // clear content between each query
    document.getElementById("valid").innerHTML = "";
    document.getElementById("visualization").innerHTML = "";
    // get textarea content
    var query = document.getElementById("tarea").value;
    // Create AJAX request
    if (window.XMLHttpRequest) {
        var xhr = new XMLHttpRequest();
    } else {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.open("POST", "custom.php", true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send("query=" + encodeURI(query));
    xhr.onreadystatechange=function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                // success
                document.getElementById("visualization").innerHTML = xhr.responseText;
            }
            else {
                // error
                document.getElementById("valid").innerHTML = xhr.responseText;
            }
        }
    }
    return false;
}
</script>

要测试执行超时,可以在header:之后添加

ini_set('max_execution_time',1 ); 
sleep(3);