AJAX另一个PHP脚本运行时不会调用它的PHP脚本 [英] AJAX wont call its PHP script while another PHP script is running

查看:144
本文介绍了AJAX另一个PHP脚本运行时不会调用它的PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案:

我有两个PHP脚本被同时调用:

  1. 在第一个脚本将运行几分钟(基于PHP文件下载),根据下载的文件大小
  2. 第二个PHP脚本应该被称为内定期监督执行的第一个脚本 - 文件进行下载。为了避免打开脚本完成后,新的窗口,它是通过AJAX调用。

问题:

在经常被称为AJAX的监控脚本不是第一次长时间运行PHP(后下载)脚本的执行过程中进行处理。只有当第一个脚本完成了AJAX称为PHP脚本得到处理。

我花了很多时间在这个问题。我简化了我的测试脚本尽可能地。不过,我仍然不能得到AJAX脚本执行主PHP脚本中工作。我也不能获得中介反馈值从主下载脚本,以任何其他方式。

你会这么好心,分析我的code样本吗?他们有precise形式,我使用它们了。如果可能的话,你会这么好心,并在你的环境中运行呢?我怀疑问题可能是在我WAMP环境。

  • 在PHP版本5.4.12
  • 的Apache / 2.4.4(Win64中)PHP / 5.4.12
  • 在Windows 7的64
  • 8GB内存

code的例子:

JavaScript的code调用这两个PHP脚本:

 <!DOCTYPE HTML>
< HTML>
< HEAD>
    <标题>将文档和LT的名称; /标题>
< /头>

<身体的onload =callScripts();>


<脚本类型=文/ JavaScript的>

    //调用这两个PHP脚本(下载,监控等)所需顺序
    callScripts =功能()
    {
        //运行长时间运行(后下载)的PHP脚本
        的console.log(调用:PHP / fileDownload.php);
        window.location.href ='PHP / fileDownload.php;

        //调用监控PHP脚本中多次2秒的间隔
        window.setTimeout(函数(){startDownloadMonitoring()},1000);
        window.setTimeout(函数(){startDownloadMonitoring()},3000);
        window.setTimeout(函数(){startDownloadMonitoring()},5000);
        window.setTimeout(函数(){startDownloadMonitoring()},7000);
        window.setTimeout(函数(){startDownloadMonitoring()},9000);
    };


    //调用监控PHP通过AJAX脚本
    功能startDownloadMonitoring()
    {
        的console.log(调用startDownloadMonitoring()...);

        VAR XMLHTTP;

        如果(window.XMLHtt prequest)
        {// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
            XMLHTTP =新XMLHtt prequest();
        }
        其他
        {// code对IE6,IE5
            XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
        }

        xmlhttp.onreadystatechange =功能()
        {
            如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200)
            {
                的console.log(收到的响应:+ xmlhttp.responseText);
            }
        }
        xmlhttp.open(GET,PHP / fileDownloadStatus.php,真正的);
        xmlhttp.send();
    }
< / SCRIPT>
< /身体GT;
< / HTML>
 

的PHP脚本监控(fileDownloadStatus.php)

 < PHP

包括ChromePhp.php;

//启动会话,更新会话变量,关闭会话
在session_start();
$ _SESSION ['DownloadProgress'] ++;
ChromePhp ::日志($ _ SESSION [\'DownloadProgress \'] ='$ _SESSION ['DownloadProgress']);
session_write_close();

回声成功;
?>
 

PHP长期运行脚本(fileDownload.php)

 < PHP
包括ChromePhp.php;

//禁用脚本到期
或者set_time_limit(0);

//启动会话,定义变量,关闭会话
在session_start();

// prepare会话变量
$ _SESSION ['DownloadProgress'] = 10;

session_write_close();

// 60秒执行
为($计数= 0; $计数< 60; $计数++)
{
    睡眠(1);
}

?>
 

解决方案

这不是送过阿贾克斯的第一个脚本:

  //运行长时间运行(后下载)的PHP脚本
  的console.log(调用:PHP / fileDownload.php);
  window.location.href ='PHP / fileDownload.php;
 

您只需将用户重定向到另一个页面,因为你有下载头在PHP中,文件下载在同一页面中。

您可以很容易地通过 IFRAME 实现你的范围。您可以设置该iframe的来源:的PHP / fileDownload.php,然后只需拨打您的AJAX下载检查

短的例子:

 < IFRAME SRC =PHP / fileDownload.php>

<脚本>
        window.setTimeout(函数(){startDownloadMonitoring()},1000);
        window.setTimeout(函数(){startDownloadMonitoring()},3000);
        window.setTimeout(函数(){startDownloadMonitoring()},5000);
        window.setTimeout(函数(){startDownloadMonitoring()},7000);
        window.setTimeout(函数(){startDownloadMonitoring()},9000);
        // ....等等等等
< / SCRIPT>
 

Scenario:

I have two PHP scripts to be called simultaneously:

  1. First script will run several minutes (PHP based file download), depending on downloaded file size
  2. Second PHP script is supposed to be called within regular intervals to monitor execution of the first script - file progress download. To avoid opening new windows upon script completion, it is called via AJAX.

Problem:

The regularly called AJAX monitoring script is not processed during the execution of the first long running PHP(later download) script. Only if the first script is finished the AJAX called PHP script gets processed.

I spent many hours over this problem. I have simplified my test scripts as much as possible. However, I still can not get the AJAX script working during execution of the main php script. Neither can I obtain intermediary feedback values from the main-download script, in any other way.

Would you be so kind and analyze my code samples please? They have the precise form as I use them now. If possible, would you be so kind and run them in your environment? I suspect the problem can be in my WAMP environment.

  • PHP Version 5.4.12
  • Apache/2.4.4 (Win64) PHP/5.4.12
  • Windows 7 x64
  • 8GB RAM

Code Samples:

JavaScript code calling both PHP scripts:

<!DOCTYPE html>
<html>
<head>
    <title>Title of the document</title>
</head>

<body onload="callScripts();">


<script type="text/javascript">

    // call both PHP scripts(download and monitoring) in desired order
    callScripts = function()
    {
        // run long running (later Download) PHP script
        console.log("Calling: PHP/fileDownload.php");
        window.location.href = 'PHP/fileDownload.php';

        // call the monitoring PHP script multiple times in 2 second intervals
        window.setTimeout(function(){startDownloadMonitoring()}, 1000);
        window.setTimeout(function(){startDownloadMonitoring()}, 3000);
        window.setTimeout(function(){startDownloadMonitoring()}, 5000);
        window.setTimeout(function(){startDownloadMonitoring()}, 7000);
        window.setTimeout(function(){startDownloadMonitoring()}, 9000);
    };


    // call monitoring PHP script via AJAX
    function startDownloadMonitoring()
    {
        console.log("Calling startDownloadMonitoring()...");

        var xmlhttp;

        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                console.log("Response Received: " + xmlhttp.responseText);
            }
        }
        xmlhttp.open("GET", "PHP/fileDownloadStatus.php", true);
        xmlhttp.send();
    }
</script>
</body>
</html>

PHP Monitoring Script(fileDownloadStatus.php)

<?php

include 'ChromePhp.php';

// start session, update session variable, close session
session_start();
$_SESSION['DownloadProgress']++;
ChromePhp::log('$_SESSION[\'DownloadProgress\'] = ' . $_SESSION['DownloadProgress']);
session_write_close();    

echo "success";
?>

PHP long-running script (fileDownload.php)

<?php
include 'ChromePhp.php';

// disable script expiry
set_time_limit(0);

// start session, define variable, close session
session_start();

// prepare session variables
$_SESSION['DownloadProgress'] = 10;

session_write_close();

// execute for 60 seconds    
for( $count = 0; $count < 60; $count++)
{
    sleep(1);
}

?>

解决方案

The first script it's not send through ajax:

 // run long running (later Download) PHP script
  console.log("Calling: PHP/fileDownload.php");
  window.location.href = 'PHP/fileDownload.php';

You simply redirect the user to another page, and because you have download headers in php, the file is downloaded in the same page.

You can easily achieve your scope through an iframe. You set the source of that iframe : 'PHP/fileDownload.php' and then simply call your ajax download checker.

Short example:

<iframe src="PHP/fileDownload.php">

<script>
        window.setTimeout(function(){startDownloadMonitoring()}, 1000);
        window.setTimeout(function(){startDownloadMonitoring()}, 3000);
        window.setTimeout(function(){startDownloadMonitoring()}, 5000);
        window.setTimeout(function(){startDownloadMonitoring()}, 7000);
        window.setTimeout(function(){startDownloadMonitoring()}, 9000);
        // .... blah blah
</script>

这篇关于AJAX另一个PHP脚本运行时不会调用它的PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆