代码很简单,但是找不到解决方法,主机提供商也解决不了,这里求助各位大侠了:<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>eShop Tools</title>
</head>
<body>
<?php
  ini_set('display_errors',0);
  error_reporting(E_ERROR | E_WARNING | E_PARSE);  $max_execution_time = ini_get('max_execution_time');
  ini_set("max_execution_time", 300);  file_put_contents("log111.txt",date("Y-m-d H:i:s")."=>begin while"."\r\n",FILE_APPEND);  $StartTime = date("y-m-d h:i:s");
  while(true){
    $EndTime   = date("y-m-d h:i:s");
    $TimeSpent = strtotime($EndTime)-strtotime($StartTime);    $i=0;
    do{
       $i++;
    }while($i<100000);    if( $TimeSpent >120){
       break;
    }
  }  file_put_contents("log111.txt", date("Y-m-d H:i:s")."=>before echo"."\r\n" ,FILE_APPEND);  ini_set("max_execution_time", $max_execution_time);  echo "Success";  file_put_contents("log111.txt", date("Y-m-d H:i:s")."=>after echo"."\r\n" ,FILE_APPEND);?>
<input type='button' name='upload' id='upload' value='upload' onclick="return exec_ajax();" /><div id="loading" style="position:fixed;position:absolute;top:40%;left:40%;width:134px;height:200px;overflow:hidden;background:url(images/ajax-loader.gif) no-repeat;z-index:10;display:none;">Processing...</div></body>
</html>************************************
上面的脚本运行后,得到的日志文件log111.txt内容如下:
2015-12-26 10:46:31=>begin while
2015-12-26 10:48:32=>before echo
2015-12-26 10:48:32=>after echo从日志可以看出PHP代码是全部执行了,但是从浏览器端看不出任何内容输出,依然显示正在执行,查看浏览器源代码时,就只是到<body> ,其后就没有内容了。主机空间的max_execution_time = 600.如果我把  $TimeSpent >120 改成  $TimeSpent >20 的话,一切正常,浏览器能够输出 “Success”和其余html代码。 这明显是和时间有关,但不知道是和哪一个时间设置有关。以上这段代码,在本地的环境执行正常,在IX Web Hosting也正常。但是上传到SiteGround主机空间上就无法输出,像挂死在那里一样不动,也不退出。SiteGround方面也给不出解决办法。
不知道哪一位有没有遇到过这样的现象? 我折腾了两天了。

解决方案 »

  1.   

       $i=0;
        do{
           $i++;
        }while($i<100000);    if( $TimeSpent >120){
           break;
        }
      }为什么要这样写呢?
      

  2.   

    nginx里面有个超时时间的设置
    虚拟主机的话你是改不了配置的
    而且用户操作也不能那么久,改配置支持不是好的实现方案
    把复杂的操作放到队列吧
      

  3.   

    事实上,我需要利用ajax实现到后台抓去并更新数据,但是测试的时候就发现服务端的程序无法返回给ajax,日志记录的情况和我的测试脚本是一样的:<?php
    ini_set('display_errors',0);
    error_reporting(E_ERROR | E_WARNING | E_PARSE);
    if(isset($_REQUEST['action'])){
    $action   = $_REQUEST['action'];
      $max_execution_time = ini_get('max_execution_time');
      ini_set("max_execution_time", 1000);
      file_put_contents("log111.txt",date("Y-m-d H:i:s")."=>begin upload"."\r\n" ,FILE_APPEND);  $StartTime = date("y-m-d h:i:s");
      while(true){
        $EndTime   = date("y-m-d h:i:s");
        $TimeSpent = strtotime($EndTime)-strtotime($StartTime);    $i=0;
        /* some operation here */
        sleep(1);    if( $TimeSpent >150){
         break;
        }
      }  file_put_contents("log111.txt",date("Y-m-d H:i:s")."=>upload finish"."\r\n",FILE_APPEND);
      file_put_contents("log111.txt", date("Y-m-d H:i:s")."=>before echo"."\r\n" ,FILE_APPEND);  die("Success");
      file_put_contents("log111.txt", date("Y-m-d H:i:s")."=>after echo"."\r\n",FILE_APPEND);  ini_set("max_execution_time", $max_execution_time);  exit("success11111");
    }  //end ajax call_user_func?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>eShop Tools</title>
    </head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script><script type="text/javascript">
      $(document).ready(function(){  });  //ajax call
      function exec_ajax(act) {    $("#loading").show();//display loading    $.ajax({
          type: "GET",
          url: 'ajtest.php',
          cache:false,
          data: {"action": "upload"},
          dataType: 'text',
          complete:function(){
            $("#loading").hide();    //hide loading
          },
          success: function(msg){
           alert( msg );
          },
          error: function(XMLHttpRequest,textStatus,errorThrown){
             alert(XMLHttpRequest.status);
             alert(XMLHttpRequest.readyState);
             alert(textStatus);
          }
        });   //ajax call end
      }
    </script><body>
    <?php  $max_execution_time = ini_get('max_execution_time');
      ini_set("max_execution_time", 300);  $StartTime = date("y-m-d h:i:s");
      while(true){
        $EndTime   = date("y-m-d h:i:s");
        $TimeSpent = strtotime($EndTime)-strtotime($StartTime);    $i=0;
        do{
         $i++;
        }while($i<100000);    if( $TimeSpent >120){
         break;
        }
      }  file_put_contents("log111.txt",date("Y-m-d H:i:s")."=>upload finish"."\r\n",FILE_APPEND);
      file_put_contents("log111.txt", date("Y-m-d H:i:s")."=>before echo"."\r\n" ,FILE_APPEND);  ini_set("max_execution_time", $max_execution_time);  echo "Success";  file_put_contents("log111.txt", date("Y-m-d H:i:s")."=>after echo"."\r\n" ,FILE_APPEND);?>
    <input type='button' name='upload' id='upload' value='upload' onclick="return exec_ajax();" /><div id="loading" style="position:fixed;position:absolute;top:40%;left:40%;width:134px;height:200px;overflow:hidden;background:url(images/ajax-loader.gif) no-repeat;z-index:10;display:none;"><p style ="background:#F5F5F5;line-height:50px;text-align:center;margin-top:100px;font-size:22px;color:#800000;z-index:1;">Processing...</p>
    </div></body>
    </html>
    不是ajax接收不到返回,而是php里面的echo和exit根本就没有起作用。而且往往过了八九分钟后,server端的这段php代码会自动再运行一遍,真是奇怪。
      

  4.   

    不好意思,上面代码粘贴了两遍, 用sleep和用while 效果是一样的。
      

  5.   

    不知道你贴出的代码是一个文件还是几个文件
    也不知道那段是 ajtest.php
      

  6.   

    是有些乱,不好意思。 重新传一下:<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>eShop Tools</title>
    </head>
    <body>
    <?php
      ini_set('display_errors',1);
    //  error_reporting(E_ERROR | E_WARNING | E_PARSE);  $max_execution_time = ini_get('max_execution_time');
      ini_set("max_execution_time", 300);  file_put_contents("log111.txt",date("Y-m-d H:i:s")."=>begin while"."\r\n",FILE_APPEND);  $StartTime = date("y-m-d h:i:s");
      while(true){
        $EndTime   = date("y-m-d h:i:s");
        $TimeSpent = strtotime($EndTime)-strtotime($StartTime);    $i=0;
        do{
         $i++;
        }while($i<100000);    if( $TimeSpent >120){
         break;
        }
      }  file_put_contents("log111.txt", date("Y-m-d H:i:s")."=>before echo"."\r\n" ,FILE_APPEND);  ini_set("max_execution_time", $max_execution_time);  echo "Success";  file_put_contents("log111.txt", date("Y-m-d H:i:s")."=>after echo"."\r\n" ,FILE_APPEND);?>
    <input type='button' name='upload' id='upload' value='upload' onclick="return exec_ajax();" /><div id="loading" style="position:fixed;position:absolute;top:40%;left:40%;width:134px;height:200px;overflow:hidden;background:url(images/ajax-loader.gif) no-repeat;z-index:10;display:none;">Processing...</div></body>
    </html>我用while和用sleep都试过,结果一样,没有输出。