How can PHP be used to send a file to a client after a password input?
To send a file to a client after a password input, you can create a PHP script that checks if the correct password is entered by the user. If the password is correct, the script can use headers to force the browser to download the file. You can store the password in a secure manner, such as using a hashed version in a database.
<?php
$correctPassword = "your_password_here";
if(isset($_POST['password']) && $_POST['password'] === $correctPassword) {
$file = 'path_to_your_file.pdf';
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
exit;
} else {
echo "Incorrect password. Please try again.";
}
?>
Related Questions
- What are some common reasons for PHP scripts to work on a web server but not on an intranet server?
- In what situations would it be more appropriate to use a PHP file with a header of "text/css" instead of a traditional CSS file?
- What are some best practices for handling form validation in PHP to ensure all fields are filled before executing further actions?