How to go about protecting files from unauthorized downloads

后端 未结 3 1697
旧时难觅i
旧时难觅i 2021-01-01 05:35

I am creating a membership site using PHP and a MySQL database, I have the means for users to log in using their username and password. All pretty standard stuff.

I

相关标签:
3条回答
  • 2021-01-01 05:42

    Here is an answer if you are having issues with .htaccess

    Ok, so if you are serving this is public_html, go to it's parent directory create a directory name "videos". (if not in public_html, you may have to add some ../ to the path). The pathinfo takes care of the validation issue and will return only a file name if someone tries to sneak a path in there. If you name it download.php, this:

    download.php?video=fun.mp4

    will load a file called fun.mp4 from the video directory.

    <?php
    // do your user authentication
    $video_directory = "../videos/";
    $file = pathinfo($_GET["video"], PATHINFO_BASENAME);
    if( $user_is_logged_in )
    {
        if( file_exists( $file ) readfile( $video_directory . $file );
    }
    
    ?>
    
    0 讨论(0)
  • 2021-01-01 05:48

    USe simple .htaccess/htpasswd authentication, digest or basic:

    http://www.askapache.com/online-tools/htpasswd-generator/

    0 讨论(0)
  • 2021-01-01 06:01

    You can protect your video folder with an .htaccess file and 'route' all the requests through a php script:

    RewriteEngine on
    RewriteRule (.*) index.php?file_id=$1 [L]
    

    And do the authentication in the index.php

    session_start();
    // get filename from database or somewhere else
    $filename = getFilename($_GET["file_id"]);
    
    if ($_SESSION["is_logged_in"]) {
        readfile($filename);
    }
    
    0 讨论(0)
提交回复
热议问题