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