2013-05-28 34 views
1

我正在嘗試創建php流應用程序。 問題是我不能讓兩個用戶在同一時間連接,因爲他們支付了它,所以如果他們分享流是無用的。如何檢測用戶是否中止了連接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

一個可能的解決方案,這將是執行該腳本作爲一個線程在後臺與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

http://phptutorial.info/?proc-open

+0

Tnx,我不知道這是答案,但我會研究它! – user1606732

0

我的意思是說,你應該包括最後一條if語句中的那些行使用else語句,因此用戶僅在連接中止時退出。

if(!connection_aborted()) 
    { 
    goto stream; 
    } 
else 
    user::logout($username); 
    exit();` 
    } 

如果你知道你流可以打破的字節數已經transmited給用戶之後的連接 內容的文件大小。

+0

Tnx,抱歉,但我不明白我應該放在哪裏,它在文件的末尾! – user1606732

+0

我想說,你應該在最後一條if語句中包含這些行,使用else語句,所以用戶只有在連接被中止時纔會註銷。 – Gimmy

+0

好的,但我通常不會那樣做,因爲它們是不必要的。如果用戶如果仍然連接,它將再次流回,這意味着除了下載流之外,它不可能做任何其他事情。另一方面,如果用戶真的斷開連接,它將註銷並退出。反正Tnx! – user1606732

相關問題