Check if a file was included or loaded
Quoted from: How to know if php script is called via require_once()?
I was looking for a way to determine if a file have been included or called directly, all from within the file. At some point in my quest I passed through this thread. Checking various other threads on this and other sites and pages from the PHP manual I got enlightened and came up with this piece of code:
if (basename(__FILE__) == basename($_SERVER["SCRIPT_FILENAME"])) {
echo "called directly";
} else {
echo "included/required";
}
In essence it compares if the name of the current file (the one that
could be included) is the same as the file that is beeing executed.
Credit: @Interwebs Cowboy
PHP: Check if a file is loaded directly instead of including?
If you use
define('APP_RAN');
in the file that includes it and then put
if(!defined('APP_RAN')){ die(); }
or alternatively
defined('APP_RAN') or die();
(which is easier to read)
in included files it would die if you access them directly.
It would probably be better to put all of your included files above your DocumentRoot though.
For example, if your index page is at
/my/server/domain/public_html
You should put the included files in
/my/server/domain/
Is there any way to know if the file was included or loaded directly?
There are many ways, bound to be a duplicate. One way if you're not using any URL rewriting, is to check the name of the file against the file that is called. Here's one way to do that:
if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) {
//direct
} else {
//included
}
__FILE__
returns the actual file path on disk of the file, such as/var/www/mysite/GetCategories.php
$_SERVER['PHP_SELF']
returns the current file loaded from the webserver, such as/includes/GetCategories.php
or/index.php
If they are the same, then it was loaded directly. If they are not the same then the file from 2 above included the file from 1.
How to know if a JS file is loaded on server side?
Since the script is requested from your server every time a user loads a browser-page you can track who and how often that path is requested.
A simple approach is that it will be present in you request log files. So you can create a script and read your log files every so often.
A second approach is to setup a special rule/location in nginx/apache/which-ever-server-you-are-running
A third approach is to serve the script via CDN that has all these attributes built in (ie. CloudFront)
Check if a file exists before loading it
In order to see whether or not a file exists in internal local storage of the app use:
import 'dart:io' as io;
var syncPath = await path;
// for a file
await io.File(syncPath).exists();
io.File(syncPath).existsSync();
// for a directory
await io.Directory(syncPath).exists();
io.Directory(syncPath).existsSync();
Check if an include (or require) exists
I believe file_exists
does work with relative paths, though you could also try something along these lines...
if(!@include("script.php")) throw new Exception("Failed to include 'script.php'");
... needless to say, you may substitute the exception for any error handling method of your choosing. The idea here is that the if
-statement verifies whether the file could be included, and any error messages normally outputted by include
is supressed by prefixing it with @
.
Related Topics
JSON: PHP to JavaScript Safe or Not
Concat Columns with Laravel 5 Eloquent
How to Convert Float Value to Integer in PHP
Using Header() to Rewrite Filename in Url for Dynamic PDF
Unpacking an Array of Arguments in PHP
Change Initial Array Inside the Foreach Loop
Add a Fee to Woocommerce Per Product, Based on Category
How to Connect to Database on Another Server
Best Way to Parse an Invalid HTML in PHP
PHP Check If Time Is Between Two Times Regardless of Date
How to Send Money to Paypal Using PHP
How to Replace the Text of a Node Using Domdocument
How to Redirect to Different Url Based on Roles in Symfony 2
PHP E-Mail Form Sender Name Instead of E-Mail