ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My question in simple form:
- When you include() or require() a php script:
----how do you, in that script, know that it has been included
OR
----what $_SERVER[''] or other variable tells the INCLUDED script's name, not the "parent" script's name.
Example:
PHP Code:
/*parent.php*/ <?php //include the header, it's set to include_once, so if I am included, it will not print. include_once("header.php"); include("child.php");
/*i can be included somewhere too, i better check if i should print the footer (which contains </body></html>, but I don't know how, if I set it to include_once, I (as the lowest thing on the chain) will print it*/ include("footer.php"); ?>
/*child.php*/ <?php //include the header, it's set to include_once, so if I am included, it will not print. include_once("header.php"); include("child1.php");
/*i can be included somewhere too, i better check if i should print the footer (which contains </body></html>, but I don't know how, if I set it to include_once, I (as the lowest thing on the chain) will print it*/ include("footer.php"); ?>
etc.
I use files like "recursively included till the tree comes to an end" so my help system can be extensible. I have trouble fixing the footer, as the script has no way of knowing if it was the starter script or not.
The way mambo (mamboserver.com) solves this issue is by defining a variable in the parent, then checking it's definition in the child. I guess you could take a similar approach.
thanks for the reply. I have already tried that as a solution, but it does not work out. Why? because that only takes you a limited number of tree nodes down. I am trying a recursive one, so that is not a good solution for me. But maby I will think something up.
Maybe you can explain a little more exactly what it is you're trying to do. Why are you recursivly including stuff? What is your tree? Are you referring to the filesystem?
I'm suprised about the defining not working, but maybe you could consider a lock file of sorts? I guess though, the problem arises when you cannot access top-level defines or variables. To overcome this, a unique number may be passed into a superglobal, such as $_POST $_GET or $_SERVER through custom headers. Taking this unique number you can create a file and write into it anything you need to pass it around. Just make sure to delete it at the end of the script.
the actual files are too long to include here, and the whole thing is (sadly) not open-source.
I am not "actually" including files recursively, but the files act like a tree. The index includes a list full of includes like this:
PHP Code:
echo "<ul class='navig'>"; echo "<li class='title'>SECTION 1</li>"; include("section1.php"); echo "<li class='title'>SECTION 2</li>"; include("section2.php"); echo "</ul>"; /*each of the sections also look like this. This way, they make a tree.*/
the whole code is a lot tidyer then this, but this is a good example.
The whole thing will be used for context sensitive help: the lists have to have a stand-alone functionality. That is why each of the files gets a include_once("header.php"); and an include("footer.php"). the thing is the footer.php gets included together with the list, because I have no way of "NOT" including the footer. it would work if I could say in every file:
PHP Code:
if (!isset($starter_script)) { /*hey, I am the starter script!*/ $starter_script = $_SERVER['SCRIPT_NAME']; }
the only thing is that $_SERVER['SCRIPT_NAME'] is always the name of the starter script and not the name of the script that got included (ie, it does not change with the script. If only I could get the name of the currently executing script...)
<?php /*the very beginning of the file*/ $tree_level_id=3; /*this overrides the parent's $tree_level_id!*/ $tree_level[$tree_level_id]=true; /*set the file tree level (relative to 0: help index)*/ //now check if we are the topmost: function are_we_top($tree_level_id, $tree_level) {
for ($i = 0; i < $tree_level_id; i++) { if ($tree_level[i]) { //make bail out if (i < $tree_level_id) { return false; } else { return true; }
So if $_SERVER['SCRIPT_NAME'] always returns the originally called (and thus parent script) could you not compare it to a hardcoded copy of the current script's path, or maybe just use 'SCRIPT_FILENAME' and hardcode the filename only. If they're the same you're a stand alone instance, else you're being included elsewhere?
1st: hardcode the filename:
You heard me when i compared this script's execution to a recursive function.. the hardcoded filename has to be assigned to a variable, which has to have the same name trhu every file (and thus the child's instance overrides the hardcoded filename). if I could use different variable names for every file that would be a good solution.
2nd: the 'script_filename'
I did not know script_filename returns the filename of the child's script... it is inherently a server variable (e.g., Apache sets it when it recieves the GET or POST request... then the included files are processed directly by the php parser and the variable does not get reset. I could be wrong, (hell, i hope i am), but it's too late tonight to test this thing (damn, i believe adulthood really does have some good points ).
You would save me (and others that might come to this thread to look for info) some time and try if $_SERVER["SCRIPT_FILENAME"] changes value over includes. If you don't feel like it, i'm gonna do it and post it here tomorrow morning.
You know I would click Affero if i could sign up to paypal.. but it seems new EU members are not very welcome there either .
Good night (day, morning, whatever it is at your place)
I dont have a PHP environment installed atm, and have only messed with the language so far, but about hardcoding the name:
If you have to save the string representation of the file's name before comparing it in an if statement, is there no way of limiting the scope of the definition to the immediat testing function, or even included script?
Even if the scope is not limited, would not something like
PHP Code:
$this_file_is_called = "test.php"; if ($_SERVER['SCRIPT_NAME'] != $this_file_is_called) { return true; //it is a child }
,with the $_SERVER['SCRIPT_NAME'] apparently never changing but the variable being changed, result in a differing output if in an included script?
Also, if you do not need to store the string in a variable prior to being compared to in an if statement, then the different strings for each filename would result in different outputs whether the main $_SERVER['SCRIPT_NAME'] is the only script being executed or if it's an included script that's providing a filename to test against.
I first started to write a reply how this would not work, as I have already considered it... but then I realized that you have a point.. you would set the name of the file right before you check it, not at the beginning of the script.
I'm rusty. I really am. I haven't programmed in a while. (except for the past week). I stuck to my opinion that every "configurable" thing has to be set in the same file (or, if that is not possible, at the beginning of the file).
Although this is a great solution, i have already found a better one. (look at my previous post).
I have found the variable (actually, it's a constant, but constants are read-only variables right) __FILE__ which contains exactly what I needed: the filename of the script currently being parsed (and so, the code can know what file it's in)
Thank you all for your time and ideas (as it seems, ideas are becoming the first thing that sells, not the actual info )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.