Sunday, July 5, 2009

Creating include-only PHP files

Sometimes you just want the files to be included, but not called directly. you might want to do this for security or what other reasons. here's the simple code:

<?php

if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])){exit();}

?>


if name of the file requested is the same as the current file, the script will exit directly. place this at the top of the scripts you want to set include-only.

However, take note that if the name of your included file is the same as your main file, it will get kicked out as well. As a developer, the convention is to name your files differently. e.g. users.php and users.inc.php

1 comment:

Priya Kannan said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
PHP Training in Chennai