我有一個視頻網站,我只想讓人們每天觀看一定數量的視頻。如何限制某個IP地址每X小時的視頻觀看次數?
這樣做的最好方法是什麼?
我目前基本上是這樣的.PHP腳本:
viewvideo.php(視頻HTML)
<?
$_SESSION['video'] = 'myvid.mp4';
$_SESSION['hash'] = md5($randomhash); //not a hash of filename etc. just random
?> then in the html : <video src='video.php?hash=<?=$_SESSION['hash'];?>'>
video.php(用於視頻文件是否被允許
<?php
if (canThisIpViewVideo()) {
// can view the video ok
if ($_SESSION['hash'] == $_GET['hash']) {
// next couple of lines get the video filename (from session), add one to list of ips (so canThisIpViewVideo has data to read from), then readfile's the .mp4 vid
$videofile = $_SESSION['video'];
mysql_query("insert into viewed (ip,date) values('$ip',NOW())"); // i do sanatize input
session_destroy(); // so can only be viewed once per hash
readfile("videos/$videofile");
die();
}
這個工程確定爲短(14秒)的視頻,我的服務器,如果!眥sIpViewVideo(),但對於正常大小的視頻,它會加載一些奇怪的東西(視頻播放器不會播放它,會等待時間(20-40秒)然後播放,如果在任何時候(開始播放之前)用戶都會播放再次它會要求它(更多的視頻播放器的錯誤)
是否有任何方式在.htaccess限制請求(到一大堆(100)的視頻)到每天一定數量?
每天限制觀看次數的大多數大型網站如何做到這一點? readfile似乎不適合我。如果我將頭文件替換爲readfile(「Location:$ fullvideourl」);它工作100%很好。
(另外,我可能會取代的代碼只是一個memcached的一點mysql的請求,因爲它的工作更容易(舊數據的自動到期(超過1天))
(我可以把另一臺服務器(如節點等)的不同端口上,如果這些人會比Apache更適合)。也視頻是在/視頻受到.htpasswd保護(除了測試) – slycat 2013-03-24 15:17:10