Skip to content
PHP Logo

What are some best practices for organizing PHP files to prevent direct access through the browser?

To prevent direct access to PHP files through the browser, it is recommended to store sensitive files outside of the public web directory and use a front controller pattern to route all requests through a single entry point.

php
<?php
// Place this code in your front controller file (e.g. index.php)
if (basename(__FILE__) == basename($_SERVER['SCRIPT_FILENAME'])) {
    header("HTTP/1.0 403 Forbidden");
    exit('Direct access not allowed');
}

// Rest of your application code here