我正在尝试创建php流应用程序。 问题是我不能允许两个用户同时连接,因为他们正在为它付费,所以如果他们分享流是没用的。
所以我的解决方案是在我的MySQL数据库中添加一行,如果用户使用1登录,我会记录,当用户停止/取消流记录时,将设置回0。这就是问题所在我可以用户中止连接时检测不到。
我试过一切,我想:ignore_user_abort(true), while(!connection_aborted){readfile($stream)}...
我有多个流文件,但我会在这里放一个我认为最接近解决方案的文件。当我没有使用任何类型的动态流/用户名/密码时,O和其他一件事工作了一段时间。所以这里是文件:
<?php
require "../general/bootstrap.php"; // Include bootstrap
require "../general/error.php"; // Comertials when error
session_write_close();
ignore_user_abort(TRUE);
set_time_limit(0);
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in history
header("Content-Transfer-Encoding: binary");
header("Cache-control: no-cache");
header('Pragma: no-cache');
if (count($items) != 3) {
if (isset($items[1]) && !empty($items[1])) {
$username = $items[1];
} else {
$username = null;
}
error("Missing one or more arguments(tv, username, password)!", $username);
}
$channel = (string) $items[0];
$username = (string) $items[1];
$password = (string) $items[2];
if (stream::channel_exists($channel)) {
if (stream::channel_is_tv($channel)) {
header('Content-Disposition: attachment; filename="stream.xspf"');
} else {
header('Content-Disposition: attachment; filename="stream.m3u"');
}
} else {
header('Content-Disposition: attachment; filename="stream.xspf"');
}
if (!stream::user_has_channel($username, $channel)) {
error("User has requested channel '$channel' that it doesn't have!", $username);
}
if (!user::login($username, $password)) {
error("Login failed! - probably because of trying to log in from 2 places at the same time or invalid username/password combination!", $username);
}
if (!isset($stream))
$stream = stream::get_channel_stream($channel); // returns http://xxx.xxx.xxx.xxx/channel
function flush_buffers() {
ob_end_flush();
ob_flush();
flush();
//ob_start();
fastcgi_finish_request();
}
// STREAM START
stream:
while(!connection_aborted() && user::is_enabled($username)) {
readfile($stream);
flush_buffers();
}
//STREAM END
sleep(10);
if(!connection_aborted()){
goto stream;
}
user::logout($username);
exit();`
答案 0 :(得分:1)
如果我理解正确,这就是你需要的: http://php.net/manual/en/function.connection-aborted.php
答案 1 :(得分:0)
一种可能的解决方案是将脚本作为线程执行,并在后台使用exec(),shell_exec或proc_open()运行它。这样,您可以监视进程并轻松检测脚本是否已成功退出或已中止。我建议使用proc_open()作为您可以轻松打开和监控管道。
以下是一些处理proc_open()
的有用链接http://php.net/manual/en/function.proc-open.php
http://www.molecularsciences.org/PHP/proc_open_tutorial_and_examples
答案 2 :(得分:0)
我想说,你应该使用else语句在最后一个if语句中包含这些行,这样只有在连接中止时才会注销用户。
if(!connection_aborted())
{
goto stream;
}
else
user::logout($username);
exit();`
}
如果您知道要传输的内容的文件大小,则可以断开连接 在将字节数传输给用户之后。