hi im trying to make parts for the website i want to build and it's will be like: header.php, footer.php, etc... I want these files to work only when i include it and no one can directly access them. is there any way to do that please?
Here are two options you could give a try:
<?php
/**
* Returns true if current file is included
*/
function isIncluded() {
$f = get_included_files();
return $f[0] != __FILE__;
}
if(!isIncluded()) {
// Do some stuff, eg: print some HTML
} else {
// Show 403/error
}
?>
<?php
// You can also use (recommended)
if(__FILE__ !== $_SERVER["SCRIPT_FILENAME"]) {
// this file is being included
}
?>
You may also opt to put the files into a directory protected by a .htaccess
and a Deny from all
since PHP can bypass that, but users cannot.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With