【发布时间】:2013-03-14 01:41:10
【问题描述】:
我有一个视频网站,我只想让人们每天观看一定数量的视频。
最好的方法是什么?
我目前基本上是在 .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();
}
如果 !canThisIpViewVideo(),这对于我服务器的短(14 秒)视频可以正常工作,但对于正常大小的视频,它加载很奇怪(视频播放器要么不会播放,会等待很长时间(20-40 秒)然后播放,如果在任何时候(在它开始播放之前)用户再次点击播放,它将再次请求它(更多来自视频播放器的错误)
.htaccess 中是否有任何方法可以将请求(对一大堆 (100) 视频)限制为每天一定数量?
大多数限制每日浏览量的大型网站是如何做到这一点的? readfile 似乎对我不起作用。如果我将 readfile 替换为 header("Location: $fullvideourl");它可以 100% 正常工作。
(另外,我可能只用一段 memcached 代码替换 mysql 请求,因为它更容易工作(旧数据自动过期(超过 1 天))
【问题讨论】:
-
(如果其中一个比 apache 更合适,我可以将另一台服务器(如节点等)放在不同的端口上)。视频也位于 /videos 中,受 .htpasswd 保护(测试除外)