Protect PDF docs from being directly accessed in URL

前端 未结 3 1170
孤独总比滥情好
孤独总比滥情好 2020-12-30 17:37

I want to protect a pdf file from being directly linked but instead have my logged in users be able to access it. I have a link which currently goes to a javascript functio

相关标签:
3条回答
  • 2020-12-30 18:04

    This is kinda sloppy but it could work assuming you can setup a MYSQL DB. It lets you pass the "password" in the URL as an MD5 string or as a clear text if you want to. Trying to setup some kind of security without using htaccess or an existing frame work is kinda clunky. This however won't even attach the file to the stream until it knows you've been "Authenticated" I think you could maybe make this a little better if you setup a login page that saved a cookie locally then you wouldn't need to pass the "passphrase" in the URL.

        $file = $_GET['file'];
        $pass = $_GET['pass'];
        $download_folder = '../Protected';
        $file = basename($file);
        $filepath = "$download_folder/$file";
    
        if (file_exists($filepath)) {
            if(CheckUser($pass)){
                header("Content-type: application/octet-stream");
                header("Content-Disposition: attachment; filename=$file");
                session_write_close();
                readfile($filepath);
            } else {
                echo 'Not Authenticated!';
            }
        } else {
            echo 'No File!';
        }
    
    function CheckUser($value){
        $con = mysqli_connect("test.com","test","123456","my_db");
        // Check connection
        if (mysqli_connect_errno()){
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        $result = mysqli_query($con,"SELECT user FROM pass_table WHERE password =".md5($value).";");
        while($row = mysqli_fetch_array($result)){
            mysqli_close($con);
           //return $row['user'];
           if($row['user']){
               return true;
           }
        }
        mysqli_close($con);
        return false;
    
    }
    
    0 讨论(0)
  • 2020-12-30 18:21

    The best way would be to protect that folder with htaccess, as you have mentioned. So you put all PDFs in pdf/ folder, and in the same pdf folder you out .htaccess file:

    RewriteEngine on
    RewriteRule .* your-php-script.php
    

    Now no files can be accessed by url in this folder. Every request to every file in this folder will return what your-php-script.php script returns. In your-php-script.php you do something like this:

    //Check if user has right to access the file. If no, show access denied and exit the script.
    $path = $_SERVER['REQUEST_URI'];
    $paths = explode('/', path);
    $lastIndex = count($paths) - 1;
    $fileName = $paths[$lastIndex]; // Maybe add some code to detect subfolder if you have them
    // Check if that file exists, if no show some error message
    // Output headers here
    readfile($filename);
    

    Now if user opens domain.com/pdf/nsa-secrets.pdf Apache will run your-php-script.php. Script will have variable $_SERVER['REQUEST_URI'] set to "domain.com/pdf/nsa-secrets.pdf". You take the last part (filename) and output it to a user (or not).

    This will stop anyone from accessing files directly from the internet by knowing URL. If someone has direct access to files on your server, that will not stop them. On the other hand, I think any shared hosting stops users from getting files of other clients. Only way to do it is to hack the server in some way. But then we are getting very paranoid and if that may be a case for you, you shouldn't use shared hosting in the first place.

    If you cannot make htaccess work, you can try to obfuscate files, so it would be difficult to spot them for someone outside. For example change file from mySecretData.pdf to djjsdmdkjeksm.pdf. This may help a little bit.

    0 讨论(0)
  • 2020-12-30 18:22

    I want to protect a pdf file from being directly linked but instead have my logged in users be able to access it.

    Check to ensure there is an authenticated user before streaming the PDF's content.

    0 讨论(0)
提交回复
热议问题