0

我有一个视频网站,我只想让人们每天观看一定数量的视频。

做这个的最好方式是什么?

我目前基本上是在 .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 天))

4

1 回答 1

2

Limiting to IP address is bad idea. For example, in Latvia high school has one IP. behind that ip is about 10000 users. The same is with large companies, that have one public IP. But if You want do that, then just log IP address, when users open video. And before showing video, check how many times from that IP already watched video. If limit is reached, then show that to user. And in DB you can count only views.
mysql_query("UPDATE views SET count = count + 1 WHERE date = '$today' AND ip = '$IP'") if this query returns 0, then you must insert row for that ip for today.

Edit: See this: Limit user access to user identified by IP address only 5 times a day

于 2013-03-24T15:27:12.640 回答