I need to provide interaction between js on html-page and php-script.
- Use AJAX.
Ok. But the problem is that php-script executing for a long time, and i need to know the state of this script processing (e.g. 60% complete)
What should i do? Create 2 php-scripts (client&server) and do ajax-request to client.php which will do requests to server.php via sockets or smth? Are there more elegant solutions?
我需要在html-page和php-script上提供js之间的交互。——使用AJAX。好的。但是问题是php脚本执行了很长时间,我需要知道这个脚本处理的状态(例如60%完成),我应该做什么?创建两个php脚本(client&server)并对客户机执行ajax请求。php,用于对服务器进行请求。php通过套接字还是smth?有更优雅的解决方案吗?
6 个解决方案
#1
2
What if you had the script doing the processing write its status to a file once in awhile. Make a second script that will read the file and return the status of the original one.
如果让脚本执行处理,那么每隔一段时间将其状态写到文件中。创建第二个脚本,读取文件并返回原始文件的状态。
#2
2
You should never have a long-running process being executed entirely within an HTTP session.
您不应该让一个长期运行的进程在HTTP会话中完全执行。
A simple and common approach to this problem is message queuing. Basically, you have your UI queue up the request into a database table and then have external daemon(s) process the queue.
解决这个问题的一个简单而常见的方法是消息排队。基本上,您的UI将请求排队到数据库表中,然后让外部守护进程处理队列。
To provide feedback, have the daemon periodically update the table with the status for the row it's currently working on. Then, your javascript code can make AJAX requests to a script that retrieves the status for that work item from the database and displays it to the user.
为了提供反馈,让守护进程定期更新表,其中包含当前正在处理的行的状态。然后,javascript代码可以向脚本发出AJAX请求,该脚本从数据库中检索工作项的状态并将其显示给用户。
See: Dealing with long server-side operations using ajax?
参见:使用ajax处理长服务器端操作?
#3
2
- Ajax call php script and return information that script is runing.
- Ajax调用php脚本并返回脚本正在运行的信息。
- Main script create lock.file.
- 主脚本创建lock.file。
- Script called from cron is checking if lock.file exists and run the correct script.
- 从cron调用的脚本正在检查是否锁定。文件存在并运行正确的脚本。
- The correct script saves the current progress into progress.txt.
- 正确的脚本将当前的进度保存到progress.txt中。
- Ajax is reading progress.txt and when progress is 100% then return information that script processing is finished.
- Ajax是阅读的进展。当进度为100%时,返回脚本处理完成的信息。
edited: Thanks to Justin for poiting the timeout problem ;)
编辑:感谢Justin提出超时问题;)
#4
1
If you want to be really fancy, write output from the php script to stdout, and capture it via a pipe. This would require running the php script using exec() or proc_open() (http://php.net/manual/en/function.proc-open.php) and pipe the output to a file (or if you want to be extra-extra fancy, use node.JS to listen for that data)
如果您想要更花哨,可以从php脚本编写输出到stdout,并通过管道捕获它。这将需要使用exec()或proc_open() () (http://php.net/manual/en/function.proc-open.php)运行php脚本,并将输出传输到一个文件(或者如果您希望更花哨,可以使用node)。监听数据)
There are quite a few ways to accomplish this:
有很多方法可以做到这一点:
- Node.JS
- node . js
- An Ajax query every x seconds
- 每x秒有一个Ajax查询
- A META/Javascript page reload
- 元/ Javascript加载页面
- An iframe that is routinely reloading with the status in it.
- 一个iframe,它经常重新加载其中的状态。
Good luck!
好运!
#5
0
You could use PHP's output buffering (see ob_flush) to flush the contents at certain points in your script and tailor your JavaScript so that it uses the flushed contents. I believe readyState in your AJAX call won't be set to 4 on flushes so that's where you'll have to handle that yourself (see this article). I think its a much nicer way than writing to a file and checking the contents of that.
您可以使用PHP的输出缓冲(参见ob_flush)在脚本的某些位置刷新内容,并调整JavaScript,以便使用已刷新的内容。我相信AJAX调用中的readyState不会设置为4 on flushes,因此您必须自己处理这个问题(请参阅本文)。我认为这是一种更好的方式,而不是写到一个文件并检查它的内容。
#6
0
on your process.php
:
在你process.php:
// 1st task
$_SESSION['progress'] = 0;
// your code for the first task here ...
// 2nd task
$_SESSION['progress'] = 10;
// you code for 2nd task ...
// 3rd task
$_SESSION['progress'] = 17;
// continue ...
// everything finished?
$_SESSION['progress'] = 100;
on your progress.php
:
在你progress.php:
// simply output
echo $_SESSION['progress'];
now from your client-side, just make a request to your progress.php
, receive the number and give it to your progress bar ...
现在,从您的客户端,对您的进度发出一个请求。php,收到号码并把它给你的进度条……
didn't check that by myself, but hope that it works! :)
我自己没有检查过,但希望它能起作用!:)
#1
2
What if you had the script doing the processing write its status to a file once in awhile. Make a second script that will read the file and return the status of the original one.
如果让脚本执行处理,那么每隔一段时间将其状态写到文件中。创建第二个脚本,读取文件并返回原始文件的状态。
#2
2
You should never have a long-running process being executed entirely within an HTTP session.
您不应该让一个长期运行的进程在HTTP会话中完全执行。
A simple and common approach to this problem is message queuing. Basically, you have your UI queue up the request into a database table and then have external daemon(s) process the queue.
解决这个问题的一个简单而常见的方法是消息排队。基本上,您的UI将请求排队到数据库表中,然后让外部守护进程处理队列。
To provide feedback, have the daemon periodically update the table with the status for the row it's currently working on. Then, your javascript code can make AJAX requests to a script that retrieves the status for that work item from the database and displays it to the user.
为了提供反馈,让守护进程定期更新表,其中包含当前正在处理的行的状态。然后,javascript代码可以向脚本发出AJAX请求,该脚本从数据库中检索工作项的状态并将其显示给用户。
See: Dealing with long server-side operations using ajax?
参见:使用ajax处理长服务器端操作?
#3
2
- Ajax call php script and return information that script is runing.
- Ajax调用php脚本并返回脚本正在运行的信息。
- Main script create lock.file.
- 主脚本创建lock.file。
- Script called from cron is checking if lock.file exists and run the correct script.
- 从cron调用的脚本正在检查是否锁定。文件存在并运行正确的脚本。
- The correct script saves the current progress into progress.txt.
- 正确的脚本将当前的进度保存到progress.txt中。
- Ajax is reading progress.txt and when progress is 100% then return information that script processing is finished.
- Ajax是阅读的进展。当进度为100%时,返回脚本处理完成的信息。
edited: Thanks to Justin for poiting the timeout problem ;)
编辑:感谢Justin提出超时问题;)
#4
1
If you want to be really fancy, write output from the php script to stdout, and capture it via a pipe. This would require running the php script using exec() or proc_open() (http://php.net/manual/en/function.proc-open.php) and pipe the output to a file (or if you want to be extra-extra fancy, use node.JS to listen for that data)
如果您想要更花哨,可以从php脚本编写输出到stdout,并通过管道捕获它。这将需要使用exec()或proc_open() () (http://php.net/manual/en/function.proc-open.php)运行php脚本,并将输出传输到一个文件(或者如果您希望更花哨,可以使用node)。监听数据)
There are quite a few ways to accomplish this:
有很多方法可以做到这一点:
- Node.JS
- node . js
- An Ajax query every x seconds
- 每x秒有一个Ajax查询
- A META/Javascript page reload
- 元/ Javascript加载页面
- An iframe that is routinely reloading with the status in it.
- 一个iframe,它经常重新加载其中的状态。
Good luck!
好运!
#5
0
You could use PHP's output buffering (see ob_flush) to flush the contents at certain points in your script and tailor your JavaScript so that it uses the flushed contents. I believe readyState in your AJAX call won't be set to 4 on flushes so that's where you'll have to handle that yourself (see this article). I think its a much nicer way than writing to a file and checking the contents of that.
您可以使用PHP的输出缓冲(参见ob_flush)在脚本的某些位置刷新内容,并调整JavaScript,以便使用已刷新的内容。我相信AJAX调用中的readyState不会设置为4 on flushes,因此您必须自己处理这个问题(请参阅本文)。我认为这是一种更好的方式,而不是写到一个文件并检查它的内容。
#6
0
on your process.php
:
在你process.php:
// 1st task
$_SESSION['progress'] = 0;
// your code for the first task here ...
// 2nd task
$_SESSION['progress'] = 10;
// you code for 2nd task ...
// 3rd task
$_SESSION['progress'] = 17;
// continue ...
// everything finished?
$_SESSION['progress'] = 100;
on your progress.php
:
在你progress.php:
// simply output
echo $_SESSION['progress'];
now from your client-side, just make a request to your progress.php
, receive the number and give it to your progress bar ...
现在,从您的客户端,对您的进度发出一个请求。php,收到号码并把它给你的进度条……
didn't check that by myself, but hope that it works! :)
我自己没有检查过,但希望它能起作用!:)