每次循环执行时都可以回显吗?例如:
foreach(range(1,9) as $n){
echo $n."\n";
sleep(1);
}
Run Code Online (Sandbox Code Playgroud)
我不想在循环结束时打印所有内容,而是希望每次打印每个结果.
我已经尝试了几次尝试让我的flush和ob_flush工作.我已经尝试设置ini以允许缓冲,我尝试使用我在网上找到的几个不同的功能来进行输出缓冲,而且根本没有任何功能正常工作.该脚本希望等到它完成后直到它回显输出.这是我到目前为止的脚本
ob_start();
//Login User
echo 'Logging in to user<br>';
ob_flush();
flush();
$ch = curl_init("http://www.mysite.com/login/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&pass=$pass");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies/$cookie");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies/$cookie");
$output = curl_exec($ch);
curl_close($ch);
ob_flush();
flush();
//Update Status
echo 'Updating Status<br>';
ob_flush();
flush();
$ch = curl_init("http://www.mysite.com/update/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "status=$status");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies/$cookie");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies/$cookie");
$output = curl_exec($ch);
curl_close($ch);
ob_flush();
flush();
Run Code Online (Sandbox Code Playgroud)
我希望它回应它正在做的事情,然后运行该函数,然后回显其他东西,然后做另一个函数.我希望在浏览器上实时刷新和回显所有缓冲区.
注意:我知道这看起来像是这个问题的副本.我不知道它是如何复制的.我已经说过我已经检查了这个问题,我尝试了所有的答案,但它没有用.
这是我尝试过的:
根据Dario的回答,我按照步骤进行了操作,但没有在名称部分找到任何 php
在接受的答案上,我在命令行提示符下运行相同的代码,但它显示:
appcmd.exe set config /section:handlers "/[name='PHP55_via_FastCGI'].ResponseBufferLimit:0"
Run Code Online (Sandbox Code Playgroud)
并返回:
错误(消息:找不到请求的集合元素.)
然后我检查了PHP手册:
我没有找到像_via_FastCGI那样的配置文件中的任何内容
我检查了Plesk的PHP设置,是的,我的服务器在FastCGI模式下运行.
所以,我已经在我的开发服务器上工作了很多个小时用于该冲洗部分,现在在我的实时服务器上它无法正常工作.任何帮助将受到高度赞赏
更新1:
正如@artlung所建议的,我在这里附上了截图:
更新2:
这是我通过搜索关键字"FastCGI"在notepad ++中找到的内容
<add name="FastCgiModule" lockItem="true" />
<add name="PleskEngine-FastCGI-html" path="*.html" verb="*" modules="FastCgiModule" scriptProcessor="C:\Program Files (x86)\Parallels\Plesk\admin\bin\php-cgi.exe|-dauto_prepend_file=auth.php" resourceType="Either" responseBufferLimit="0" />
<add name="PleskEngine-FastCGI-html" path="*.html" verb="*" modules="FastCgiModule" scriptProcessor="C:\Program Files (x86)\Parallels\Plesk\admin\bin\php-cgi.exe|-dauto_prepend_file=auth.php" resourceType="Either" responseBufferLimit="0" />
<add name="PleskEngine-FastCGI-php3" path="*.php3" verb="*" modules="FastCgiModule" scriptProcessor="C:\Program Files (x86)\Parallels\Plesk\admin\bin\php-cgi.exe|-dauto_prepend_file=auth.php" resourceType="Either" responseBufferLimit="0" />
<add name="PleskEngine-FastCGI-php3" path="*.php3" verb="*" modules="FastCgiModule" scriptProcessor="C:\Program Files (x86)\Parallels\Plesk\admin\bin\php-cgi.exe|-dauto_prepend_file=auth.php" resourceType="Either" responseBufferLimit="0" />
<add name="PleskEngine-FastCGI-php" path="*.php" verb="*" modules="FastCgiModule" scriptProcessor="C:\Program Files …Run Code Online (Sandbox Code Playgroud) 我开始说这在我的本地机器上完美运行,下面的js示例连接到stream.php并且每秒接收服务器当前时间的连续更新.
var source = new EventSource("stream.php");
source.addEventListener('message', function(e) {
console.log(e);
}, false);
source.addEventListener('open', function(e) {
console.log(e);
}, false);
source.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED) {
console.log('closed');
}
}, false);
Run Code Online (Sandbox Code Playgroud)
while(true)
{
// Headers must be processed line by line.
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
// Set data line
print "data: " . date( 'G:H:s', time() ) . PHP_EOL . PHP_EOL;
// Toilet
flush();
// Wait one second.
sleep(1);
}
Run Code Online (Sandbox Code Playgroud)
上传到live dev之后我确实预计会有一点延迟.服务器.但是有大约15到20分钟的时间延迟.在我看到第一个条目之前.
连接不会丢失.(问题.已经过了40分钟+现在.)这只是一个Apache循环问题(意味着是时候查看网络套接字)还是我可以做些什么来解决这个问题?
我目前正在为网页游戏编写通信框架,通信地图如下所示:
代码如下:
test.php的:
<!DOCTYPE html>
<html>
<head>
<title> Test </title>
<script>
function init()
{
var source = new EventSource("massrelay.php");
source.onmessage = function(event)
{
console.log("massrelay sent: " + event.data);
var p = document.createElement("p");
var t = document.createTextNode(event.data);
p.appendChild(t);
document.getElementById("rec").appendChild(p);
};
}
function test()
{
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function ()
{
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200)
{
console.log("reciver responded: " + xhr.responseText);
}
}
xhr.open("GET", "reciver.php?d=" + document.getElementById("inp").value , true);
xhr.send();
console.log("you sent: " + document.getElementById("inp").value); …Run Code Online (Sandbox Code Playgroud) 请原谅一个看起来很简单的问题,可能对PHP / sleep()的工作方式缺乏了解,但是我对以下代码有所了解:
<?php
$time_now = time();
echo "Time Now : " . $time_now;
sleep(10);
$time_then = time();
echo "<br>Time Then : " . $time_then;
?>
Run Code Online (Sandbox Code Playgroud)
我期望此代码输出当前时间,然后等待10秒,然后在10秒后输出时间。
而是等待设置的10秒,并同时输出两个时间。时间是正确的,因为“ Time Now”是我执行代码的时间,而“ Time Then”是10秒后的时间,但是它没有响应我的预期(输出Time Now,等待10秒,然后继续执行代码并显示“时间到了”)。
鉴于我的理解显然是错误的,是否有可能实现这一目标?还是我最好触发另一个文件来加载我想做的事情,如下所示:
文件1
<?php
$time_now = time();
echo "Time Now : " . $time_now;
header('Location: page2.php');
?>
Run Code Online (Sandbox Code Playgroud)
文件2
<?php
sleep(10);
$time_then = time();
echo "<br>Time Then : " . $time_then;
?>
Run Code Online (Sandbox Code Playgroud)
谢谢。