LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-18-2004, 12:34 PM   #1
atom
Member
 
Registered: Feb 2004
Location: Slovenia
Distribution: archlinux
Posts: 271

Rep: Reputation: 31
php include()/require() script question


Hi, and thanks for the answers in advance.


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.


Atom
 
Old 08-18-2004, 01:56 PM   #2
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
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.
 
Old 08-18-2004, 02:01 PM   #3
atom
Member
 
Registered: Feb 2004
Location: Slovenia
Distribution: archlinux
Posts: 271

Original Poster
Rep: Reputation: 31
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.
 
Old 08-18-2004, 02:09 PM   #4
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
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?
 
Old 08-18-2004, 02:13 PM   #5
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
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.
 
Old 08-18-2004, 02:27 PM   #6
atom
Member
 
Registered: Feb 2004
Location: Slovenia
Distribution: archlinux
Posts: 271

Original Poster
Rep: Reputation: 31
I am building a help system.

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...)
 
Old 08-18-2004, 02:53 PM   #7
atom
Member
 
Registered: Feb 2004
Location: Slovenia
Distribution: archlinux
Posts: 271

Original Poster
Rep: Reputation: 31
I think I know how to solve it!

PHP Code:
<?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$tree_level_idi++) {
     if (
$tree_level[i]) {
          
//make bail out
          
if ($tree_level_id) {
                return 
false;
          } else { return 
true; }
          
     }
}
}/*end function*/

this should do it...
 
Old 08-18-2004, 03:05 PM   #8
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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?
 
Old 08-18-2004, 05:43 PM   #9
atom
Member
 
Registered: Feb 2004
Location: Slovenia
Distribution: archlinux
Posts: 271

Original Poster
Rep: Reputation: 31
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)
 
Old 08-18-2004, 06:25 PM   #10
atom
Member
 
Registered: Feb 2004
Location: Slovenia
Distribution: archlinux
Posts: 271

Original Poster
Rep: Reputation: 31
I found it guys! (actually some1 showed it to me...) it's right here: http://www.php.net/manual/en/languag...predefined.php

__FILE__

Hope that helps some other ppl then me.
 
Old 08-18-2004, 06:36 PM   #11
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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.

Last edited by Proud; 08-18-2004 at 06:39 PM.
 
Old 08-19-2004, 01:12 AM   #12
atom
Member
 
Registered: Feb 2004
Location: Slovenia
Distribution: archlinux
Posts: 271

Original Poster
Rep: Reputation: 31
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 )
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP include it-s Programming 3 10-05-2005 11:07 PM
require shell script cranium2004 Programming 1 12-11-2004 12:34 AM
NavBar and <?php require Gerardoj Programming 2 04-13-2004 02:04 PM
PHP Question: how to make include(); depending on URL? linuxfond Programming 7 09-05-2003 09:15 AM
php - include, require and arrays gui10 Programming 1 02-05-2002 08:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration