Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7736913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:51:52+00:00 2026-06-01T07:51:52+00:00

With PHP, I am developing a script that generates a contract once the script

  • 0

With PHP, I am developing a script that generates a contract once the script is validated.

The contract is a pdf document generated with TCPDF and I save it to the server in a subdirectory with the user’s ID. For example, ‘contracts/132/1.pdf’ would be bill #1 of user with ID 132.

However, I want only user 132 to be able to access that file, because it contains personal information. How can I limit the access to pdf documents in each subfolder to their respective user (using php or htaccess, whichever works best – I’m not very familiar with htaccess)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:51:53+00:00Added an answer on June 1, 2026 at 7:51 am

    The easiest way is probably just to have a PHP script that requires the existence of a valid session (like the generator script does), whose function is to readfile("/path/to/contract.pdf");

    That way, you can have your PDF wrapper script verify that the contract being downloaded is the RIGHT contract for the person in the sesion, not just that it’s a contract that is in the directory.

    The problem with a .htaccess-based solution on the directory is that anyone with read access to the directory can download ANY contract.

    Given a URL like http://example.com/contract.php?user=132&bill=1 you could:

    <?php
    
    $user = $_GET['user'];
    $bill = $_GET['bill'];
    # do input validation on $user and $bill.  No really, do it.
    
    if ($user != $_SESSION['user']) {
      die("Security error; the black choppers are on their way.");
    }
    
    header("Content-type: application/pdf");
    header('Content-Disposition: attachment; filename="Contract-$user-$bill.pdf"');
    
    readfile("/path/to/pdfspool/$user/Contract-$user-$bill.pdf");
    

    The if () chunk in the middle verifies that the $user being requested is valid for the current user. Obviously, you’ll want to store $_SESSION[‘user’], probably when this user first logs in.

    Of course, you don’t NEED to keep spool files, really. If the process of generating a PDF isn’t going to overwhelm your web server (and if it does you have other problems), it may just be easier to re-generate each PDF from scratch, on request. That’s what I do with company invoices now, and each invoice gets a 6 point footer saying when it was generated and by a request from what IP address. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have a php script located on a sever that generates a JSON
I am developing a PHP script that has to make some calls to a
I am developing a PHP script for uploading .PDF documents as medium BLOBs into
I'm developing a PHP script that loops/iterates more that 10,000 times: foreach ($array_with_items as
I am developing some php script that works fine if I access it by
I'm developing a long running php script that compiles scraped information from multiple sources,
I am developing a PHP application using our XAMPP setup as a test server.
I am developing a PHP application that will be run only on the local
I have developed a PHP script that uses $_REQUEST[] superglobal. A typical client request
I am developing a semi-complicated site that is available in several countries at once.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.