problem with PHP_SELF

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
here's situation:

i have installed php 5 and apache 2.0.

i can call my page with:


inside my page i use links like this:

<a href="<?php $_SERVER['PHP_SELF']?>?style=bold">

to refresh page with new value to variable "style". Problem is that when i
load my page and try to click that link it directs me to:


- "localhost" is missing, and therefore browser reports that page is
unavailable. If i set my link to

<a href="page.php?style=bold">

it's ok, but i'd like to use php_self.

is that maybe problem with apache configuration?


Re: problem with PHP_SELF

Quoted text here. Click to load it

The variable $_SERVER['php_self'] doesn't contain the "http://" prefix, and
if you were to prepend "http://" to $_SERVER['php_self'], you would get the
behavior you describe above.  In the above instance, $_SERVER['php_self']
would be set to "subdir/page.php".  So if you use it like this:

<a href="<?php echo $_SERVER['PHP_SELF']?>?style=bold">

....then you shouldn't be having any problem.  Since what you wrote above as
your code won't produce _any_ output (since you didn't echo out the
variable), I assume that you retyped rather than copy/pasted and thus
introduced another error.  Retyping your code on the newsgroup is very bad
practice and should be avoided, at it usually just adds to the confusion of
troubleshooting errors in your code.

The documentation for $_SESSION and all other reserved variables can be
found at [ ].  I would suggest reading
it, as it is rather helpful for questions like this.

Hope this helps.


Re: problem with PHP_SELF

i'at work so it's different name...
 thanks for replay. i did some reading and came to this "new" problem.

(sorry i mistyped code before, (i forgot to put "echo"))

so... i'm using
<a href="<?php echo $_SERVER['PHP_SELF']?>?style=bold">
as links to reload page. and it works if my page is at root:
( "C:\program files\apache group\apache2\htdocs\page.php")
bit if my page is in some subdir it doesn't
( "C:\program files\apache group\apache2\htdocs\subdir\page.php")
can i still use 'PHP_SELF' to get only name "page.php" not URL relative to

sorry for my lousy english.

Re: problem with PHP_SELF

Quoted text here. Click to load it

it looks like this works:


thanks anyway.

Re: problem with PHP_SELF

*** Maja escribió/wrote (Fri, 14 Jan 2005 20:34:28 +0100):
Quoted text here. Click to load it

I can't figure out what you mean with this :-?

Quoted text here. Click to load it

There's no "echo", I suppose it's just a typo.

Quoted text here. Click to load it

From PHP manual < :


The filename of the currently executing script, relative to the document
root. For instance, $_SERVER['PHP_SELF'] in a script at the address would be /test.php/ The
__FILE__ constant contains the full path and filename of the current (i.e.
included) file.

If PHP is running as a command-line processor, this variable is not

Check the rest of the page. Also, check the phpinfo() function. There isn't
a function that returns the current URL (which is actually never sent to
the server by the client) but you can build yours adding pieces: protocol,
host name, port, path...

In any case, you don't need the full path for internal links. Just remove
the "http://" part I'm pretty sure you add.

-+ Álvaro G. Vicario - Burgos, Spain
+- (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions

Re: problem with PHP_SELF

Maja wrote:

Quoted text here. Click to load it

That is not risk-free.  PHP_SELF, as we know, is the path
component of the current resource's URI.  Given a base URI
of <http://example.invalid/foo/ and a mistyped current URI
of <http://example.invalid//foo/bar (note the two slashes
before 'foo'), PHP_SELF is <//foo/bar>.  That's a network-
path reference, which browsers should resolve to the
absolute URI <http://foo/bar>.

If you do use PHP_SELF as the value of an href, I suggest
you check it first, as you should with all input.  Your
workaround -- only using what comes after the last slash in
PHP_SELF -- avoids the issue I mentioned.

And your English, for which you needn't apologise, is
perfectly understandable.



Site Timeline