简体   繁体   English

如何针对特定IP地址限制每X小时视频的观看次数?

[英]How to limit views to a video per X hours for a certain IP address?

I have a video site and I only want to let people watch a certain amount of videos per day. 我有一个视频网站,我只想让人们每天观看一定数量的视频。

What is the best way to do this? 做这个的最好方式是什么?

I am currently basically doing this in a .php script: 我目前基本上是在.php脚本中执行此操作:

viewvideo.php (the video html) 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 (serves the video file if it is allowed 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();
}

this works ok for a short (14 sec) video that i server if !canThisIpViewVideo(), but for normal size videos it loads weird (the video player either will not play it, will wait ages (20-40 secs) then play, and if at any time (before it has started playing) a user hits play again it will request it again (more a bug from the video player) 这适用于我服务器的短暂(14秒)视频!canThisIpViewVideo(),但对于正常大小的视频,它加载怪异(视频播放器要么不播放,要等待年龄(20-40秒)然后播放,如果在任何时候(在它开始播放之前)用户再次点击播放它将再次请求它(更多来自视频播放器的错误)

Is there any way in .htaccess to limit requests (to a whole bunch (100) of videos) to a certain number per day? 在.htaccess中是否有任何方法可以将请求(一大堆(100)视频)限制为每天一定数量?

how do most big sites that limit views per day do this? 大多数限制每日观看次数的大型网站如何做到这一点? readfile just seems to not work for me. readfile似乎对我不起作用。 if i replace readfile with header("Location: $fullvideourl"); 如果我用header替换readfile(“Location:$ fullvideourl”); it works 100% fine. 它工作100%罚款。

(Also, i might replace the mysql request with just a memcached bit of code as it would work easier (auto expire of old data (over 1 days)) (另外,我可能只用一个memcached代码替换mysql请求,因为它会更容易工作(旧数据自动过期(超过1天))

Limiting to IP address is bad idea. 限制IP地址是个坏主意。 For example, in Latvia high school has one IP. 例如,在拉脱维亚,高中有一个知识产权。 behind that ip is about 10000 users. ip后面是大约10000个用户。 The same is with large companies, that have one public IP. 拥有一个公共IP的大公司也是如此。 But if You want do that, then just log IP address, when users open video. 但是如果你想这样做,那么只需记录IP地址,当用户打开视频时。 And before showing video, check how many times from that IP already watched video. 在显示视频之前,请检查该IP已观看视频的次数。 If limit is reached, then show that to user. 如果达到限制,则显示给用户。 And in DB you can count only views. 在DB中,您只能计算视图。
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. mysql_query("UPDATE views SET count = count + 1 WHERE date = '$today' AND ip = '$IP'")如果此查询返回0,则必须为今天的ip插入行。

Edit: See this: Limit user access to user identified by IP address only 5 times a day 编辑:请参阅: 限制用户通过IP地址识别的用户每天只能访问5次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在特定时间段(24小时)内阻止IP地址 - How to block IP address for certain period of time (24 hours) 从自定义分类将订单限制为一定数量的产品(每 24 小时,按 IP 地址) - Limit orders to a certain amount of products from a custom taxonomy (every 24 hours, by IP address) PHP:如何增加表行中的值以对视图进行计数并将计数限制为一个IP地址 - PHP: How to increment a value in the table row to count views and to limit counts to one IP address 网页点击计数器-正常运行,但希望将其限制为每个IP地址 - Page Hit Counter - Working but want to limit it to per IP Address 如何限制网页访问每个IP 1个用户? - how to limit webpage access to 1 user per ip? 如何使用每个组的小时数限制来对一组对象进行分组? - How to group an array of object using a limit on hours per group? 如何使用FILTER_VALIDATE_IP限制IP地址范围 - How to limit a IP address range using FILTER_VALIDATE_IP 使用MySQL / PHP / JS限制一个IP地址每小时点击一个特定链接的次数。 - Limit the amount of times an IP address can click 1 certain link an hour- using MySQL/PHP/JS. 如何在CakePHP 2.x中通过INET_ATON保存IP地址? - How to Save IP address By INET_ATON in CakePHP 2.x? 可以PHP限制基于IP地址的动作 - can php limit actions based on ip address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM