How to Disallow Direct Access to a File with PHP

Place this code at the top of your PHP script.

if ( stristr( $_SERVER['SCRIPT_NAME'], basename( __FILE__ ) ) ) {
	exit( 'No direct script access allowed' );
}