0

I am building a web app and I use am using a lot of include files to build most of my pages.

For example:

The page profile.php includes about.php, timeline.php, photos.php

Now, I want to send a user to my 404 page if they try to go to one of my include files directly. How can I do this?

Going to localhost/timeline.php should redirect the user to the 404.

My thought was to write an IF Statement in those include files that checks to see if the file is being opened directly, is that even possible?

1

3 Answers 3

2

Fast and dirty:

Define a constant inside of profile.php, and check if it exists inside of the included files:

if (!defined("SOME_CONSTANT")) {
   //Redirect or send a 404 header
}

Slower but better

Get your files which have no meaning as a standalone file in a web context out of the web root (usually a www or htdocs folder).

0

You can use a simple if statement

if(!defined("SECURE")) {
    header("HTTP/1.0 404 Not Found", true, 404);
    include("404_Not_Found.html"); //include the 404 page
    exit;
}

Making sure that on your included pages, you define the secure constant.

0

Define a constant in your front controller and check if it is set in the other files.

In your front controller at the top:

define("IN_APP");

Then in your other pages before any processing:

if(defined("IN_APP")) { die(header('HTTP/1.0 404 Not Found', true, 404)); }

A better option however is to put your other files in a subdirectory and deny access to it with a .htaccess file containing, the following code.

Order deny,allow
deny from all

This will still allow php to include the files, but will block anyone from accessing them from the web.

Not the answer you're looking for? Browse other questions tagged or ask your own question.