-1

So I work on a website and to make things easier I made specific files for every task (like: for the top menu I made menu.php) and then require(); them in the main files. All is good but I tried accessing in the browser /include/menu.php and it shows up. I don't want people to access them whenever they want, I just want to require them and to be available only through the main file.

Blue
  • 22,608
  • 7
  • 62
  • 92
Vlad
  • 3
  • 1
  • Take a look at htaccess – Simian Aug 07 '16 at 20:29
  • Welcome to Stack Overflow! It seems you have a problem with your code. However, we can't help unless we have [code or information that can reproduce the problem](//stackoverflow.com/help/mcve). Otherwise, we are just blindly guessing. – Blue Aug 07 '16 at 20:35

1 Answers1

1

The easiest way to prevent other php files from being accessed, is to define a variable in the main script:

define('IN_APPLICATION', true);

In all of your other files, simply add:

if ( !defined('IN_APPLICATION') )
    die('You cannot access this file directly.');

An alternative way is to use an .htaccess file. If your server is running apache, this is all you will need. Simply put this file in your /includes directory.

Community
  • 1
  • 1
Blue
  • 22,608
  • 7
  • 62
  • 92
  • Oh I didn't think about that. I simply made an $onSamePage = 1; variable on the main page and check if it's true on the included one. – Vlad Aug 07 '16 at 20:47