...

View Full Version : Tracking Page/Video Requests?



sakka
07-24-2008, 12:55 AM
Hello-- I have a brief but (hopefully simple) question.
I have a bunch of flash videos on my site and am hoping to 'rank' them based on how many times they have been watched, much like Youtube.
Is there a way to do this via PHP ? I was thinking if there was a way to track how many times the file has been requested on the server...

(And please let me know if I posted this in the wrong forum - I thought doing it via PHP would be my best bet.)

Thank you :o
Sakka

ShaneC
07-24-2008, 04:09 AM
Sure!

First you need a storage method. Best way is database so I'll use that in my example but alternatively you can use a file.



<?php

//We're going to put this right on our page because obviously this page has loaded meaning the video was requested!

//I'm also assuming that you've put the video's "ID" in the database to the variable $videoID. You can use another method of identification, without knowing your database hierarchy I can't tell you for certain

$sql = mysql_query( "SELECT video_hits FROM myTable WHERE videoID = '$videoID'" )
or die( mysql_error() );

$row = mysql_fetch_array( $sql ); //This will add the result fgor video_hits to the array

$row[0]++; //Since there is only one value in the array it will begin at 0. ++ counts upwards for an integer variable in PHP (FYI: -- counts one down on the variable)

//Let's now update our table
$sql = mysql_query( "UPDATE myTable SET video_hits = '$row[0]' WHERE videoID = 'videoID'" )
or die( mysql_error() );

//All done!

?>


If there's something you don't understand or need clarifying please post and let me/us know! Also, keep in mind you will want to add additional security to this, not use it verbatum (for example if you've no video in the database with the videoID, the script will FUBAR).

digital-ether
07-24-2008, 08:09 AM
Hello-- I have a brief but (hopefully simple) question.
I have a bunch of flash videos on my site and am hoping to 'rank' them based on how many times they have been watched, much like Youtube.
Is there a way to do this via PHP ? I was thinking if there was a way to track how many times the file has been requested on the server...

(And please let me know if I posted this in the wrong forum - I thought doing it via PHP would be my best bet.)

Thank you :o
Sakka

Yes, you can use PHP to track how many times a file is requested on the server.

Lets say you have a file, movie.flv

And a PHP file, stream.php

You'll want to link to stream.php which will in turn reply with movie.flv

eg:

You can use the url:


stream.php?file=movie.flv

And in stream.php


<?php


$file = $_GET['file'];

// increment downloads
increment_download($file);

// send file to http
echo file_get_contents($file);

?>

Note: increment_downloads() is a pseudo function that saves the download count. This would depend on where you save the count, in a db, file etc.

Thats a very simplistic example, in reality you want to use fopen() and fread() to read the file in chunks so you can limit download speed as well as conserve memory.
You will also want to make sure you "sanitize" $_GET['file'] so remote file inclusions aren't made.

You can also send a HTTP redirect to the browser so that you don't have to pipe the file through PHP.

eg:



stream.php?file=movie.flv


<?php

$file = $_GET['file'];

// increment downloads
increment_download($file);

// send HTTP redirect
header("Location: $file");

?>

sakka
07-24-2008, 05:44 PM
Thanks, guys! I'll give it a shot and let you know if I have any further questions. :) Thanks for the quick responses. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum