-1

I want the log in user can access the PDF file and anonymous user can not access the file from browser like www.domain.com/pdf/name.pdf

My pdf file is getting corrupted.It is gets failed when clicked for download.

I have created pdf folder in that kept my all pdf. I have html code

<ul>
<li>
    <a href="/check.php" target="_blank">Test</a>
</li>
</ul>

check.php file

    if($_SESSION[login]==true){

    $file_url = 'http://domainname.com/pdf/example.pdf';
    $filename='example.pdf';

    header("Content-Disposition: attachment; filename=" . urlencode($filename));   
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");            
    header("Content-Length: " . filesize($file_url));
$fp = fopen($file_url, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
} 
fclose($fp);
}

ht.access

RewriteEngine on
RewriteRule .* check.php
Ashwini
  • 9
  • 2

1 Answers1

5

Just add a function in your php file as below:

function protect_page(){
if(logged_in()===false){
    header('Location: protected.php');
    exit();
}
}

Check whether the user is logged in by doing

$_SESSION[login]==true

Write the above protect_page() function on the page that has link to your pdf.

And you're good to go.

Saurabh Gupta
  • 506
  • 1
  • 4
  • 18