$_SERVER['PHP_SELF'] in a included file - PHP
This is a discussion on $_SERVER['PHP_SELF'] in a included file - PHP ; A project that I work on has a index.php that includes other files
depending on certain requirements.
so index.php includes a.php;
in a.php if you echo $_SERVER['PHP_SELF'] it echos index.php and I
understand why it does this.
But, I want ...
-
$_SERVER['PHP_SELF'] in a included file
A project that I work on has a index.php that includes other files
depending on certain requirements.
so index.php includes a.php;
in a.php if you echo $_SERVER['PHP_SELF'] it echos index.php and I
understand why it does this.
But, I want echo the file name of a.php from a.php and I dont want to
add a $filename='a.php'; at the top of a.php, although I have thought of
using subversion and a find and replace to insert $filename="$Id$"; at
the top of all the files.
Does anyone know know of another way I can get a included files filename?
Sorry if I have posted this question in the past, its been a while since
I last thought of this problem.
Regards,
Clive.
Real Time Travel Connections
{No electrons were harmed in the creation, transmission or reading of
this email. However, many were excited and some may well have enjoyed
the experience.}
-
RE: [PHP] $_SERVER['PHP_SELF'] in a included file
hello,
try with the system constant __FILE__
print(__FILE__);
inside a.php should always return a.php, even though this is included into index.php.
Vincent Dupont
Principal Consultant OpenSource Competence Center
Ausy Belgium
http://www.ausy.be
-----Original Message-----
From: clive [mailto:clive@rttc.co.za]
Sent: Mon 2/26/2007 14:44
To: PHP General List
Subject: [PHP] $_SERVER['PHP_SELF'] in a included file
A project that I work on has a index.php that includes other files
depending on certain requirements.
so index.php includes a.php;
in a.php if you echo $_SERVER['PHP_SELF'] it echos index.php and I
understand why it does this.
But, I want echo the file name of a.php from a.php and I dont want to
add a $filename='a.php'; at the top of a.php, although I have thought of
using subversion and a find and replace to insert $filename="$Id$"; at
the top of all the files.
Does anyone know know of another way I can get a included files filename?
Sorry if I have posted this question in the past, its been a while since
I last thought of this problem.
Regards,
Clive.
Real Time Travel Connections
{No electrons were harmed in the creation, transmission or reading of
this email. However, many were excited and some may well have enjoyed
the experience.}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
-
Re: [PHP] $_SERVER['PHP_SELF'] in a included file
clive wrote:
> A project that I work on has a index.php that includes other files
> depending on certain requirements.
>
> so index.php includes a.php;
>
> in a.php if you echo $_SERVER['PHP_SELF'] it echos index.php and I
> understand why it does this.
>
> But, I want echo the file name of a.php from a.php and I dont want to
> add a $filename='a.php'; at the top of a.php, although I have thought of
> using subversion and a find and replace to insert $filename="$Id$"; at
> the top of all the files.
>
> Does anyone know know of another way I can get a included files filename?
>
> Sorry if I have posted this question in the past, its been a while since
> I last thought of this problem.
__FILE__ will give you the path and filename of the current file.
-Stut
-
Re: [PHP] $_SERVER['PHP_SELF'] in a included file
Thanks Vincent,Stut and Olaf. Thats __file___ is exactly what I needed 
now for another brain teaser for your collective brains, How does a
function know what file it was called from.
a.php includes functions.php
in a.php we call function test(); which is declared in functions.php
Is there anyway for test(); to echo "called from a.php" with out
passing 'a.php' or ___FILE___ as a parameter to the function?
Im busy googling now for a answer.
--
Regards,
Clive.
Real Time Travel Connections
{No electrons were harmed in the creation, transmission or reading of
this email. However, many were excited and some may well have enjoyed
the experience.}
-
RE: [PHP] $_SERVER['PHP_SELF'] in a included file
hi
I'm afraid this is not available by default.
You can try to parse the debug_backtrace() (see http://be2.php.net/manual/en/functio...-backtrace.php)
but this is probably too complex for online processing.
if there are some native functions, please tell me !!
Vincent Dupont
Principal Consultant OpenSource Competence Center
Ausy Belgium
http://www.ausy.be
-----Original Message-----
From: clive [mailto:clive@rttc.co.za]
Sent: Mon 2/26/2007 15:15
To: clive
Cc: PHP General List
Subject: Re: [PHP] $_SERVER['PHP_SELF'] in a included file
Thanks Vincent,Stut and Olaf. Thats __file___ is exactly what I needed 
now for another brain teaser for your collective brains, How does a
function know what file it was called from.
a.php includes functions.php
in a.php we call function test(); which is declared in functions.php
Is there anyway for test(); to echo "called from a.php" with out
passing 'a.php' or ___FILE___ as a parameter to the function?
Im busy googling now for a answer.
--
Regards,
Clive.
Real Time Travel Connections
{No electrons were harmed in the creation, transmission or reading of
this email. However, many were excited and some may well have enjoyed
the experience.}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
-
Re: [PHP] $_SERVER['PHP_SELF'] in a included file
clive wrote:
> Thanks Vincent,Stut and Olaf. Thats __file___ is exactly what I needed 
>
> now for another brain teaser for your collective brains, How does a
> function know what file it was called from.
>
> a.php includes functions.php
>
> in a.php we call function test(); which is declared in functions.php
>
> Is there anyway for test(); to echo "called from a.php" with out
> passing 'a.php' or ___FILE___ as a parameter to the function?
>
> Im busy googling now for a answer.
Note that the following function has not been extensively tested, and
should be treated as such. I wrote this function a long time ago to get
the calling function from a common error routine. You should be able to
modify this to get what you want...
function GetCaller($offset = 0)
{
$trace = debug_backtrace();
$retval = '';
if (is_array($trace))
{
foreach ($trace as $call)
{
if ($offset > 0)
{
$offset--;
}
else
{
if (strlen($retval) == 0)
{
$retval = str_replace($GLOBALS['rootdir'], '',
$call['file']).':'.$call['line'].' ';
}
elseif (isset($call['function']))
{
return $retval.(isset($call['class'])
?
$call['class'].'::'
:
'').$call['function'];
}
}
}
}
if (strlen($retval) == 0)
return 'unknown';
return $retval.'unknown function';
}
Hope it helps.
-Stut
-
Re: [PHP] $_SERVER['PHP_SELF'] in a included file
Vincent DUPONT wrote:
> hi
>
> I'm afraid this is not available by default.
> You can try to parse the debug_backtrace() (see http://be2.php.net/manual/en/functio...-backtrace.php)
> but this is probably too complex for online processing.
unless you are doing something *very* exotic (which almost by definition
means your skills are beyond the point whereby this list could probably help you)
there should be *no* reason for a function to ever need to know who/what/where it was
called from/by ... of course in the process of developing something like debug_backtrace()
can be very handy for helping to figure out what is going on!
that said if a function seems to need to know who called it (or anything like that)
then you probably have a bad smell in your design .. consider that a function does
something according to it's input, and that the output of a function should be predictable [given
a particular set of input parameters) then the caller, it's environment/start and/or the
execution path should have no baring on the function's result - if they did then the function's
result could no longer be considered predictable (unless your going to memorize every execution-path/state
in your application now and in the future!).
having said all that there are probably some exceptions to the rule - although I can't think
of any right now.
>
> if there are some native functions, please tell me !!
there are none - although I did read a thread on the internals mailing list that
mentioned something along these lines in the form of a request ... unlikely that it
will be included.
>
>
> Vincent Dupont
> Principal Consultant OpenSource Competence Center
> Ausy Belgium
> http://www.ausy.be
>
>
>
> -----Original Message-----
> From: clive [mailto:clive@rttc.co.za]
> Sent: Mon 2/26/2007 15:15
> To: clive
> Cc: PHP General List
> Subject: Re: [PHP] $_SERVER['PHP_SELF'] in a included file
>
> Thanks Vincent,Stut and Olaf. Thats __file___ is exactly what I needed 
>
> now for another brain teaser for your collective brains, How does a
> function know what file it was called from.
>
> a.php includes functions.php
>
> in a.php we call function test(); which is declared in functions.php
>
> Is there anyway for test(); to echo "called from a.php" with out
> passing 'a.php' or ___FILE___ as a parameter to the function?
>
> Im busy googling now for a answer.
>
-
Re: [PHP] $_SERVER['PHP_SELF'] in a included file
I don't think it does know, nor care, nor should it...
Why would it need to know that?...
You may find the info you want in a "backtrace" function at
http://php.net/
On Mon, February 26, 2007 8:15 am, clive wrote:
> Thanks Vincent,Stut and Olaf. Thats __file___ is exactly what I needed
> 
>
> now for another brain teaser for your collective brains, How does a
> function know what file it was called from.
>
> a.php includes functions.php
>
> in a.php we call function test(); which is declared in functions.php
>
> Is there anyway for test(); to echo "called from a.php" with out
> passing 'a.php' or ___FILE___ as a parameter to the function?
>
> Im busy googling now for a answer.
>
> --
> Regards,
>
> Clive.
>
> Real Time Travel Connections
>
>
> {No electrons were harmed in the creation, transmission or reading of
> this email. However, many were excited and some may well have enjoyed
> the experience.}
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?