Is there a way to prevent a user viewing an file but still use it as included to another file in PHP?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
There are many good answers in this thread - here's one that hasn't been mentioned yet.
You can name your included PHP files with an
i
in the extension, e.g.someincludedfile.phpi
and then configure Apache so that it doesn't servephpi
files. Voila.Cons:
Pros:
Personally, I would rather just move files into a directory outside the document root, but if you have a large legacy application, this could be quicker to change.
Link: http://www.ducea.com/2006/07/21/apache-tips-tricks-deny-access-to-certain-file-types/
If you want to stop it from ever being displayed when not included here's a more automated way.
This way it'll still work if you end up changing the file name or something.
Just to expand on the solutions with
$_SERVER
variables - below is a small .php file, and in the comments is a copy of abash
test I did on Ubuntu; the point being, that these variables can change quite a bit depending on the type of access, and whether symlinks are used (note also, in the code below I have to escape the '?\>
' in the echo statement, else the syntax color breaks; remove the backslash if trying the code):I'd go for Chacha102's solution.
Additionally, as your question title says «how to check», you can also do this without having to define a variable by using
Do not use any global code in your files, only functions and methods. Then there will be no need to care about include vs. direct use.