Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Tracking Page/Video Requests?

    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
    Sakka

  • #2
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    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 Code:
    <?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).
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sakka View Post
    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
    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:
    Code:
    stream.php?file=movie.flv
    And in stream.php

    PHP Code:
    <?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:


    Code:
    stream.php?file=movie.flv
    PHP Code:
    <?php

    $file 
    $_GET['file'];

    // increment downloads
    increment_download($file);

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

    ?>
    Last edited by digital-ether; 07-24-2008 at 08:11 AM.
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog

  • #4
    New Coder
    Join Date
    Nov 2007
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks, guys! I'll give it a shot and let you know if I have any further questions. Thanks for the quick responses.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •