What are the potential security risks of allowing direct access to included files in PHP?

Allowing direct access to included files in PHP can pose security risks as it exposes sensitive information and functionality to unauthorized users. To mitigate this risk, you can use a constant to define a flag in your included files and check for this flag before executing any code. This way, the included files can only be accessed when included within the main PHP file.

<?php
define('INCLUDED_FILE', TRUE);

if(!defined('INCLUDED_FILE')){
    die('Direct access not allowed');
}

// Rest of the code in the included file