Reputation: 1281
So in file first.php
I will include file original.php
with include_once
.
Problem occurs, when I try to call functions which are inside original.php
and that file is not loading if there is no user session.
Basicaly I'm include file original.php
in which are few functions im calling later in first.php
, but those functions are inaccesible due to restrictions I've set in original.php
, but now I want to add rule to original.php
to allow such requests from first.php
or any other PHP file that will include original.php
...
I wonder what could I do in this case OR could I simply add some if clause to original.php
, like:
if($fileIncluded == 'yes')
{
// do not check if user session exist etc.
}
else
{
// check for user session
}
Thanks for ideas and help in advance!
Upvotes: 3
Views: 2524
Reputation: 15044
You can set a constant in the file to be included like so:
In original.php
:
define('ORIGINAL_INCLUDED', TRUE);
Then in first.php
you check if the constant exists:
if (defined('ORIGINAL_INCLUDED'))
{
// do not check if user session exist etc.
}
else
{
// check for user session
}
Unlike get_included_files() this will work when a script includes a script. get_included_files()
will only return the files included in the main script, but not scripts included in child scripts.
Or in original.php
:
if ('original.php' == basename(__FILE__))
{
// original.php directly accessed
}
else
{
// original.php included instead
}
Upvotes: 4
Reputation: 14872
You could also define a constant, and check if it is defined later. Use define and defined functions for that. I think it's better than using normal variables.
Php doc: http://ch2.php.net/manual/en/function.defined.php and http://ch2.php.net/manual/en/function.define.php
Upvotes: 1
Reputation: 2848
PHP provides a get_included_files function that would seem to work in this scenario.
if in_array('file.php', get_included_files()) {
/* ... */
}
Upvotes: 2
Reputation: 2597
You could use the function get_included_files()
or set a variable like this:
// includedfile.php
$variable = true;
//index.php
include 'includedfile.php';
if(isset($variable)) {
// file included
} else {
// not included
}
Upvotes: 1
Reputation: 16495
Well, I am PHP newbie, but I would do
$add_file = require(file.php);
and then do
if($add_file) {echo "file is aded";} else {echo "file is not added";}
Upvotes: 1