What are the potential security risks of allowing direct browser access to files in a PHP application?
Allowing direct browser access to files in a PHP application can pose security risks such as exposing sensitive information, allowing unauthorized access to files, and potential injection attacks. To mitigate these risks, it is important to properly validate and sanitize user input, restrict access to sensitive files, and use secure file handling techniques.
<?php
// Check if the user is authorized to access the file
if($user->isAdmin()) {
// Load the file content and output it to the browser
$file = 'path/to/file.txt';
header('Content-Type: text/plain');
readfile($file);
} else {
// Redirect the user or show an error message
header('HTTP/1.1 403 Forbidden');
echo 'You are not authorized to access this file.';
}
?>