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

如何针对特定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

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。