What are the potential pitfalls of using PHP to control file downloads based on user login status?
Potential pitfalls of using PHP to control file downloads based on user login status include security vulnerabilities such as unauthorized access to files, inefficient code that can be exploited, and potential for errors in handling user authentication. To mitigate these risks, it is important to properly validate user credentials, sanitize input data, and implement secure file access controls.
<?php
session_start();
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
$file_path = 'path/to/secure/file.pdf';
if(file_exists($file_path)) {
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
readfile($file_path);
exit;
} else {
echo 'File not found.';
}
} else {
echo 'You are not authorized to access this file.';
}
?>