douchui1488 2013-03-24 15:15
浏览 296

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

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:

viewvideo.php (the video 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

<?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)

Is there any way in .htaccess to limit requests (to a whole bunch (100) of videos) to a certain number per day?

how do most big sites that limit views per day do this? readfile just seems to not work for me. if i replace readfile with header("Location: $fullvideourl"); it works 100% fine.

(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))

  • 写回答

1条回答 默认 最新

  • duanmu2941 2013-03-24 15:27
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏