How can a web server prevent the extraction of source code using methods like file_get_contents in PHP?

To prevent the extraction of source code using methods like file_get_contents in PHP, the web server can restrict access to certain directories or files by setting appropriate permissions. This can be done by configuring the server to deny access to specific files or directories, or by using techniques such as .htaccess files to restrict access. Additionally, developers can use PHP to obfuscate their code or implement server-side validation to prevent unauthorized access.

// Example code to prevent direct access to a file using file_get_contents
if (basename(__FILE__) == basename($_SERVER['SCRIPT_FILENAME'])) {
    header('HTTP/1.0 403 Forbidden');
    exit('Access denied');
}

// Your code here