Skip to content
PHP Logo

What are some methods to prevent direct access to a PHP file and redirect users back to the index page?

To prevent direct access to a PHP file and redirect users back to the index page, you can check if a specific constant or variable is defined in the file. If it is not defined, you can use the header() function to redirect the user back to the index page.

php
<?php
if(!defined('MY_CONSTANT')) {
    header("Location: index.php");
    exit();
}
?>