0

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

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
  • (I could put another server (like node etc) on a different port, if one of these would be more suitable than apache). also the videos are in /videos which is protected with a .htpasswd (apart from for testing) –  Mar 24 '13 at 15:17

1 Answers1

1

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

Community
  • 1
  • 1
Guntis
  • 496
  • 1
  • 5
  • 19
  • thanks, i know but its the only way to stop content from being downloaded too many times until they sign up. i do insert a new row (ive just put in the mysql_query() in the sample code). but the issue is about how to serve up big .mp4 video files –  Mar 24 '13 at 15:41
  • 1) check how many views have for that IP. 2) if limit is reached, redirect to other page. 3) if not, register pageview and show video. Look here: http://stackoverflow.com/questions/3697748/fastest-way-to-serve-a-file-using-php – Guntis Mar 24 '13 at 15:44