Originally posted by phprock
How to stop direct access to a file withouth my main index.php file ?
As olaf hinted on, if they know the filenames of your scripts, they can access them directly. However, storing them in another directory (possibly outside the web root) should keep you relatively safe.
Notice I said "should". If you want to be nearly 100% safe, you need to use a constant check in your included script. If this constant is not defined, the script should die. The constant should be defined in index.php, before you include your file.
Like so, maybe:
// index.php
<?php
//define a constant.
include_once("somefile.php");
?>
// somefile.php
<?php
// if constant not defined, die with message.
?>
This is the method I use all the time. As far as I can tell, there's no way to fault it.