How can PHP be used to prevent direct access to header and footer files in a website?

To prevent direct access to header and footer files in a website, you can use PHP to check if a constant is defined in the file. If the constant is not defined, the file should not be accessed directly. This can help protect sensitive information and prevent unauthorized access to these important files.

<?php
// In header.php and footer.php files
if(!defined('MY_APP')) {
    die('Direct access not allowed');
}
?>