How can external access to PHP files be restricted to prevent unauthorized inclusion?

To prevent unauthorized inclusion of PHP files, you can restrict external access by checking if a constant is defined in the included files. By defining a constant in your main PHP file and checking for its existence in the included files, you can ensure that they are only accessed through the intended entry point.

// main.php
define('MY_APP', true);

// included_file.php
if (!defined('MY_APP')) {
    die('Unauthorized access!');
}
// Your code here