gpt4 book ai didi

php - 如何限制某个 IP 地址每 X 小时观看视频的次数?

转载 作者:行者123 更新时间:2023-12-04 19:38:45 26 4
gpt4 key购买 nike

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

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

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

最佳答案

限制 IP 地址是个坏主意。例如,在拉脱维亚,高中拥有一个 IP。该ip后面是大约10000个用户。拥有一个公共(public) IP 的大公司也是如此。但是,如果您想这样做,则只需在用户打开视频时记录 IP 地址。在显示视频之前,请检查该 IP 已观看视频的次数。如果达到限制,则向用户显示。在数据库中,您只能计算 View 。mysql_query("UPDATE views SET count = count + 1 WHERE date = '$today' AND ip = '$IP'")如果此查询返回 0,那么您必须在今天为该 ip 插入行。

编辑:
见:Limit user access to user identified by IP address only 5 times a day

关于php - 如何限制某个 IP 地址每 X 小时观看视频的次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15600169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com