Redirect to index.php when directly access any php page in root folder

there is one file called reports.php . if someone directly accessed 192.168.1.1/reports.php it should redirect to index.php prevent the direct access from users to reports.php through step by step procedure only they can reach reports.php

Answer

Solution:

For ?� case you described, you need to specify a condition in the reports.php file that results in deciding to do a redirect or execute your business logic. For example, if you use get parameters you can do this:

if (!isset($_GET["step"])) {
   // do redirect
}
// do business logic

The example uses the query parameter step. You can also use the session extension built-in to php using the global variable $_SESSION

You can see how to redirect here: How do I make a redirect in PHP?

Answer

Solution:

you can use sessions, https://www.php.net/manual/en/book.session.php

on your index.php start session with name and check on everyother page if the session exist, if not redirect to index.php

Source