problem with PHP_SELF

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maja

    problem with PHP_SELF

    here's situation:

    i have installed php 5 and apache 2.0.

    i can call my page with:

    localhost\\subd ir\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:



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


  • Richards Noah \(IFR LIT MET\)

    #2
    Re: problem with PHP_SELF

    "Maja" <drazen.matiegk a@zg.t-com.hr> wrote in message
    news:cs96s3$fpo $1@ls219.htnet. hr...[color=blue]
    > here's situation:
    >
    > i have installed php 5 and apache 2.0.
    >
    > i can call my page with:
    >
    > localhost\\subd ir\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!
    >[/color]

    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


    Comment

    • Alvaro G. Vicario

      #3
      Re: problem with PHP_SELF

      *** Maja escribió/wrote (Fri, 14 Jan 2005 20:34:28 +0100):[color=blue]
      > i can call my page with:
      >
      > localhost\\subd ir\page.php[/color]

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

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

      There's no "echo", I suppose it's just a typo.
      [color=blue]
      > http://subdir/page.php?style=bold
      >
      > - "localhost" is missing, and therefore browser reports that page is
      > unavailable. If i set my link to[/color]

      From PHP manual <http://es2.php.net/manual/en/reserved.variab les.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
      --

      Comment

      • ToMeK

        #4
        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\h tdocs\page.php" )
        bit if my page is in some subdir it doesn't
        ( "C:\program files\apache group\apache2\h tdocs\subdir\pa ge.php")
        can i still use 'PHP_SELF' to get only name "page.php" not URL relative to
        root?

        sorry for my lousy english.






        Comment

        • ToMeK

          #5
          Re: problem with PHP_SELF

          "ToMeK" <t_mih@hotmail. com> wrote in message
          news:csb6f9$3gh $1@ls219.htnet. hr...[color=blue]
          > 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\h tdocs\page.php" )
          > bit if my page is in some subdir it doesn't
          > ( "C:\program files\apache group\apache2\h tdocs\subdir\pa ge.php")
          > can i still use 'PHP_SELF' to get only name "page.php" not URL relative to
          > root?[/color]

          it looks like this works:

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

          thanks anyway.


          Comment

          • John Dunlop

            #6
            Re: problem with PHP_SELF

            Maja wrote:
            [color=blue]
            > inside my page i use links like this:
            >
            > <a href="<?php $_SERVER['PHP_SELF']?>?style=bold" >[/color]

            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

            Comment

            Working...