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