Reply to Re: 'nested conditional' that can identify parent page?

Your name:

Reply:


Posted by Alan Jones on 05/14/07 01:04

On 13 May 2007 04:41:19 -0700, shimmyshack <matt.farey@gmail.com>
wrote:

>On May 13, 3:44 am, Alan Jones <a...@jalanjones.com> wrote:
>> On 12 May 2007 19:20:20 -0700, shimmyshack <matt.fa...@gmail.com>
>> wrote:
>>
>> >> Thank you very much for the help. :) I'll give your recommendation
>> >> a run thru, but is there a way to make basename, or a similar
>> >> function, simply return the filename of the parent page; the page
>> >> the include is in? Thanks again, I really appreciate any help I can
>> >> get.
>>
>> >basename($_SERVER['SCRIPT_FILENAME']);
>> >does exactly that, try it.
>>
>> It returns the filename of the file it is in. I ran...
>>
>> echo basename($_SERVER['SCRIPT_FILENAME']);
>>
>> ...from within the 'include' file index_body.php and it returned
>> that same filename.
>>
>> I need the code in the include file to somehow determine, or derive,
>> the name of its 'parent' file in a given instance. This procedure
>> would be happening within the include file as it resides in the
>> parent file.
>>
>> Again, thanks for racking your brain on this with me. I'm at a total
>> loss... :(
>
>That wasnt quite what I expected you to do, I expected you to place
>this:
>echo basename($_SERVER['SCRIPT_FILENAME']);
>inside the included file and run the website as you would normally, if
>the included file was indeed included, then you will see a value
>different from that of the included name.
>
>I am not sure what you are asking, but I think you are saying
>1: "I have files a,b,c... each of which include u and I want to
>include e(signature) inside u only when the "top" file is b"
>but you could be saying
>2: "I have files a,b,c... each of which include u,v,w and I want to
>include e(signature) only inside v" - in which case there is an
>equally simple answer. just let me know which question you are asking
>cos I keep answering 1 and were not getting anywhere! :)
>
>Ultimately there are many ways to skin a cat, but this one just seemed
>the easiest.
>
>To see what the value of the variables are do this:
>(this will let you convince yourself that you /can/ use this method)
>
>create a file called 1.php, and inside put this:
><?php
>echo '<pre>';
>echo 'i am file called: ' . __FILE__ . "\n";
>echo 'request uri: ' . $_SERVER["REQUEST_URI"] . "\n";
>echo 'script name: ' . $_SERVER["SCRIPT_NAME"] . "\n";
>echo 'php self: ' . $_SERVER["PHP_SELF"] . "\n";
>echo 'script filename: ' . $_SERVER["SCRIPT_FILENAME"] . "\n";
>echo "\ni am going to include the next file\n";
>include ( '2.php' );
>?>
>
>create a file called 2.php and inside put this:
><?php
>#echo '<pre>';
>echo 'i am file called: ' . __FILE__ . "\n";
>echo 'request uri: ' . $_SERVER["REQUEST_URI"] . "\n";
>echo 'script name: ' . $_SERVER["SCRIPT_NAME"] . "\n";
>echo 'php self: ' . $_SERVER["PHP_SELF"] . "\n";
>echo 'script filename: ' . $_SERVER["SCRIPT_FILENAME"] . "\n";
>echo "\ni am going to include the next file\n";
>include ( '3.php' );
>?>
>
>create a file called 3.php and inside put this:
><?php
>#echo '<pre>';
>echo 'i am file called: ' . __FILE__ . "\n";
>echo 'request uri: ' . $_SERVER["REQUEST_URI"] . "\n";
>echo 'script name: ' . $_SERVER["SCRIPT_NAME"] . "\n";
>echo 'php self: ' . $_SERVER["PHP_SELF"] . "\n";
>echo 'script filename: ' . $_SERVER["SCRIPT_FILENAME"] . "\n";
>echo "\ni wont include any more files, see how despite the name of the
>file changing the others dont, this is because each file is contained
>within the first.\n";
>?>
>
>save them to the same php enabled web accessible folder and in your
>browser call up 1.php
>when you view the results you will see what you should have seen the
>first time.
>
>Here's the solution using the above method but writing it at the top
>in index.php (we are still taling about question 1 here).
>1.php
><?php
>$myname = basename(__FILE__);
>include('2.php');
>?>
>
>2.php
><?php
>if($myname=='index.php')
>{
> include_once('signature.php');
>}
>include('3.php');
>?>
>
>or if you want it to appear in an even more nested include so your
>head will explode when you come to edit this website in the future
>3.php
><?php
>if($myname=='index.php')
>{
> include('signature.php');
>}
>?>
>
>and so on,
>the basic concept is that you need logic somewhere to compare the
>value of the script filename with the value you want, and either
>have the logic just before the include telling it to be include when
>script_filename = index.php
>have the logic in the "top" script, and set a variable which ripples
>through includes and test for it.

All of your effort is very appreciated, Matt. I'm grateful for the
time you have put into this, but I think I have figured it out.

Apparently, $_SERVER does not like the use of a http web file path
when doing an include. I had been using http://jalanjones.com/...
to specify the path to the file. For the heck of it, just to try
anything, I changed the path to the local file system syntax,
'includes/index_ body.php', and it now works as it should.

I'm using...

if ($_SERVER['PHP_SELF'] == '/index.php'){include('signature.php');}

....without any errors. If I change 'index' to anything else, for
example 'index_2', it does not include the signature or give an
error. The short script is page specific and can be used, with
modified paths, in any file. That's just my speed; keep it simple
for stupid. :) I have a lot of learning to do and you helped me get
over this hump, thank you.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация