By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

problem with PHP_SELF

P: n/a
here's situation:

i have installed php 5 and apache 2.0.

i can call my page with:

localhost\\subdir\page.php

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:

http://subdir/page.php?style=bold

- "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?

thanks!
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Maja" <dr*************@zg.t-com.hr> wrote in message
news:cs**********@ls219.htnet.hr...
here's situation:

i have installed php 5 and apache 2.0.

i can call my page with:

localhost\\subdir\page.php

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:

http://subdir/page.php?style=bold

- "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?

thanks!


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 [http://www.php.net/reserved.variables]. I would suggest reading
it, as it is rather helpful for questions like this.

Hope this helps.

-Noah
Jul 17 '05 #2

P: n/a
*** Maja escribió/wrote (Fri, 14 Jan 2005 20:34:28 +0100):
i can call my page with:

localhost\\subdir\page.php
I can't figure out what you mean with this :-?

<a href="<?php $_SERVER['PHP_SELF']?>?style=bold">
There's no "echo", I suppose it's just a typo.
http://subdir/page.php?style=bold

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


From PHP manual <http://es2.php.net/manual/en/reserved.variables.php>:

'PHP_SELF'

The filename of the currently executing script, relative to the document
root. For instance, $_SERVER['PHP_SELF'] in a script at the address
http://example.com/test.php/foo.bar would be /test.php/foo.bar. 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
available.
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
+- http://www.demogracia.com (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
--
Jul 17 '05 #3

P: n/a
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
root?

sorry for my lousy english.


Jul 17 '05 #4

P: n/a
"ToMeK" <t_***@hotmail.com> wrote in message
news:cs**********@ls219.htnet.hr...
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
root?


it looks like this works:

substr(strrchr($_SERVER['PHP_SELF'],'/'),1)

thanks anyway.
Jul 17 '05 #5

P: n/a
Maja wrote:
inside my page i use links like this:

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


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.

HAGW!

--
Jock
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.