PHP Archive

Define Or Die

Want to stop a PHP file from being opened dir­ectly instead of through another? This can be par­tic­u­larly use­ful for PHP files that depend on another or just to keep secur­ity tight.

So how do this work? PHP allows you to define some­thing and you can then check to see if it is defined some­where else in the code.

Should be one of the very first, if not the first, lines in your main file.

// Let's define it!
define('ABC');

Place this at the top of every other page that you want to be forced through the main one.

if(!defined('ABC'))
	die();

Although it seems so simple, you’d be sur­prised at how many scripts aren’t pro­tec­ted. Word­Press, for instance, doesn’t which allows you to look at theme files amongst other things.