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

3 comments:

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

Sankar lp said...

I have read your blog its very attractive and impressive. I like it your blog.

PHP 7 Training in chennai | PHP 7 Training Course

PHP 7 Training in chennai | Online PHP 7 Course

ajay prakash said...

Very nice blog, Thank you for providing good information.
aviation training in chennai
cabin crew training institute in chennai
airlines training chennai
airport ground staff training in chennai
medical coding training
fashion designing courses in chennai part time
interior design courses in chennai part time