What methods can be employed in PHP to prevent the display of .inc files when accessed directly?
When accessing .inc files directly, sensitive information such as database credentials or configuration settings may be exposed. To prevent this, one common method is to place the .inc files outside of the web root directory, so they cannot be accessed directly through the browser. Another method is to use PHP code to check if the file is being accessed directly and then redirect or display an error message.
<?php
// Check if the file is being accessed directly
if(basename(__FILE__) == basename($_SERVER['SCRIPT_FILENAME'])) {
// File is being accessed directly, redirect or display error message
header("HTTP/1.0 403 Forbidden");
echo "Access forbidden";
exit();
}
// Rest of your code here