我正在使用下面的代码向用户返回mp4视频,我想在用户看到视频时更新用户的上一个活动。我想使用下面的第二个代码进行更新,但如果我返回视频,则js不会运行。
PHP代码:
$file_name = 'path/to/video.mp4';
$file_size = (string)(filesize($file_name));
header('Content-Type: video/mp4');
header('Accept-Ranges: bytes');
header('Content-Length: '.$file_size);
header("Content-Disposition: inline;");
header("Content-Range: bytes .$file_size");
header("Content-Transfer-Encoding: binary\n");
header('Connection: close');
readfile($file_name);
JS代码:
<script>
setTimeout(updateLastActivity, 10000);
function updateLastActivity(){
$.post("./sys/api.php", {option: "update_last_activity", user_id: "'.$user['id'].'", channel_id: "'.$channel['id'].'", user_agent: "'.$user_agent.'", user_ip: "'.$user_ip0.'"}).done(function(response) {}, "json");
}
</script>
答案 0 :(得分:0)
我能够通过此代码获得所需的结果,现在只需要通过异步方法调用更新上一个活动的方法。
$file_name = "path/to/mp4/file.mp4";
$file_size = (string)(filesize($file_name));
header("Content-Type: video/mp4");
header("Accept-Ranges: bytes");
header("Content-Length: $file_size");
header("Content-Disposition: inline;");
header("Content-Range: bytes $file_size");
header("Content-Transfer-Encoding: binary");
header("Connection: close");
$user_agent = (isset($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : 'not_found';
$user_ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : 'not_found';
ob_clean();
ob_end_flush();
$last_time = 0;
$handle = fopen($file_name, "rb");
while (!feof($handle)) {
if(time() > $last_time) {
$last_time = strtotime("+15 seconds", time());
/* CALLING THE METHOD TO UPDATE THE LAST ACTIVITY! */
update_last_activity($user['id'], $channel['id'], $user_agent, $user_ip);
}
echo fread($handle, 1000);
}